Configure OneDrive Files On-Demand states using PowerShell
When using the OneDrive sync client with the Files On-Demand feature enabled, your folders and files can be in one of three states, each indicated by a status icon in File Explorer.
Icon | State | Description |
---|---|---|
Online-Only | Indicates that the file is only available online. Online-only files don’t take up space on your computer but can only be accessed when your device is connected to the internet. | |
Locally Available | When you open an online-only file, it downloads to your device and becomes a locally available file. You can open a locally available file anytime, even without Internet access. | |
Always Available | An always available file is always downloaded and stored on the device. |
Understanding attributes
We can check the attributes of a given file using the command attrib.exe or with native powershell. Each method represents the attributes of the file differently.
attrib.exe
Microsoft gives you this link in regards to querying and setting files on-demand states in windows.
Each of the aforementioned states corresponds to a file attribute state. To query the current attribute of a file or folder, you can use the following command:
attrib.exe <Path to file or folder>
I’ve set up 3 files/folders, structured below - each in one of the three states.
Running the attrib.exe command against each one, we get the following output:
Running the command with a /?
switch at the end, gives us more information on what each of these attributes mean.
The results returned show that there are attributes set for both cloud-only and always-available files but no attributes for a locally available file.
File State | Attribute |
---|---|
Cloud-Only | U / Unpinned |
Always available | P / Pinned |
Locally Available | None |
PowerShell
You can also use native PowerShell to view the attributes of a file with the get-item
command.
Get-ChildItem <Path to file or folder> -Force | Format-Table Name,Attributes
I have set up the following file structure, with each file in a different state, and run a script to loop though them and return its associated attributes.
Folder structure:
Script:
get-childitem |
ForEach-Object {
write-host "$_ :" $_.Attributes
}
Output:
Example Folder: Directory, ReparsePoint
alwayslocal.txt: 525344
local.txt: Archive, ReparsePoint
online.txt: 5248544
Using PowerShell, we get a different representation of attributes for each state than what we saw with the attrib.exe command.
File State | Attribute |
---|---|
Cloud-Only | 5248544 |
Always available | 525344 |
Locally Available | ReparsePoint |
Changing attributes
You can use the attrib.exe command to change attributes associated with a file or folder. You can use +p/-p
to assign or clear the pinned attribute, and +u/-u
to assign or clear the unpinned attribute.
Files On-Demand state | File attribute state | Command |
---|---|---|
Always available | Pinned | attrib +p |
Locally available | Clearpin | attrib -p |
Online-only | Unpinned | attrib +u |
Examples
In the below example you can see the file’s initial state is Locally available. Running the first command attrib +p
you can see the file status in File Explorer changes to an Always available state. Running attrib -p
*changes the file back to a *locally available state, and running the command attrib +u
changes the file status to Cloud only.
If you want to change a file back from a Cloud only status, you can run the command attrib +p
to change it to an Always available status, but if you want to change a Cloud only status file directly to Locally available you cant just run attrib -p
.
This is because the -p
command removes a P attribute that is already associated with the file. As the file is already Cloud only it only has an attribute of U, so there is no P to remove. You first need to +p
to add the P attribute, then ‘clear the pin’ (hence the status name clearpin) with -p
.
If a folder is empty, you can change the attributes directly on the folder and the status will change; but if the folder contains anything, setting attribute at the top level folder will not change it’s status if the attributes of the files/folders contained within do not match.
PowerShell script to change all file states to online-only
If you want to change all the files in someones OneDrive for Business folder to be in an online-only state, we can use a simple PowerShell script to loop through each file and set the +U -P
attributes.
get-childitem $ENV:OneDriveCommercial -Force -File -Recurse -ErrorAction SilentlyContinue |
Where-Object {$_.Attributes -match 'ReparsePoint' -or $_.Attributes -eq '525344' } |
ForEach-Object {
attrib.exe $_.fullname +U -P /s
}
Conclusion
From the examples shown, we see that the OneDrive sync client instantly registers the attribute change and takes the required action against the file or folder.
I can’t see many use-cases where you’d want to change these file states with PowerShell. I had a fringe use-case where I needed to change a user’s files to be always-online before I scripted the device to reconfigure itself to use a different M365 tenant and OneDrive account. If you’re wanting to use a script to free up space on a device, you would be better looking at setting some storage sense policies.