Be careful when using $env:userprofile or $home to create paths to user files. When a Windows box is set up for OneDrive, the documents folder may have been redirected to a subfolder named “OneDrive”. Here are some examples:

 
PS> $env:USERPROFILE
C:\Users\tobia

PS> $HOME
C:\Users\tobia

PS> $profile
C:\Users\tobia\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

PS> 
 

As you see, the PowerShell profile script does not reside in the Documents folder directly inside the user profile. Instead, it moved into a subfolder named “OneDrive”.

To find the current Documents folder, use GetFolderPath() instead:

 
PS> [Environment]::GetFolderPath('MyDocuments')
C:\Users\tobia\OneDrive\Documents
 

You can even use this to determine whether or not OneDrive redirected user files:

$redirected = [Environment]::GetFolderPath('MyDocuments') -like '*\OneDrive\*'
$redirected 

This returns $true when OneDrive redirected folders, else $false.



Twitter This Tip! ReTweet this Tip!

Anonymous
  • In the Enterprise's with Domain Joined machines, a feature called "OneDrive for Business" is often used to provide Protected cloud storage for the users Profile data. When enable with Group Policy on both the User and Computer settings a new folder under the user profile named "OneDrive - <MyCompanyName>"  Where "MyCompanyName" would be your Azure Tenant company name.  One Feature of this technology is the use of the "Known Folder Move" redirection. When enabled it moves your Documents, Pictures and Desktop Folder from the standard location under the users profile, to under the new OneDrive for Business folder, which then puts the data in the folders in the users Cloud Storage.   So to find the users Document folder under these conditions you can use an either of 2 variables "OneDrive" or "OneDriveCommercial" which will point to the Users OneDrive Folder, if "Known Folder Redirection" is being used then the users Document folder will be there, "$ENV:OneDriveCommercial\Documents". So, if you are operating in an enterprise Active Domain Environment with Azure OneDrive feature, check for the standard location, the OneDrive personal location and/or OneDrive For Business location.