While writing a script to delete all OneDrive for Business/My Site site collections from an Office 365 tenant today, I ran up against a perplexing error using PowerShell with SharePoint Online that I thought I would document, since Googling didn’t help at all.

SPO PowerShell Error

The URL was correct, and the My Site was real and provisioned. But I kept getting a “not properly formed” error.

Well, long story short, the script I was using took a long list of URLs from a text file that had been exported from O365 admin. The exported file included a trailing forward slash at the end of each URL, and it was this slash “/” that was causing the error.

Bad: https://[Domain]-my.sharepoint.com/personal/[Account]_[Domain]_com/

Good: https://[Domain]-my.sharepoint.com/personal/[Account]_[Domain]_com

I removed the slash from all 435 items in the text file with a quick script put together for the purpose, and the error was gone!

A bit of uncommon pickiness from PowerShell…

 

Bonus: Remove last character from each line in a text file

Here’s the PowerShell script I put together to remove the slash from each line in the text file:

$file = Get-Content -Path D:\DeleteMySites\ListOfMysites.txt
for ($i=0; $i -lt $file.Count; $i++) {  $file[$i] = $file[$i].Substring(0,$file[$i].Length – 1)  }
Set-Content -Value $file -Path D:\DeleteMySites\ListOfMysites-Corrected.txt