Featured image of post Getting OneDrive File States with PowerShell

Getting OneDrive File States with PowerShell

Unlock the power of PowerShell to delve into the attributes of local OneDrive files and interpret their File On Demand states.

Navigating the World of OneDrive File Attributes

In our ever-connected digital landscape, OneDrive has remained a prominent player in the realm of cloud storage and collaboration. Managing files and syncing them across devices is generally effortless, but have you ever wondered about the nitty-gritty details of how OneDrive handles these files locally? Today, we’ll unveil the magic of PowerShell and explore how to retrieve and interpret the attributes of local OneDrive files, specifically focusing on their File On Demand (FOD) states.

Beggining Our Quest

My journey began with a simple yet intriguing task: I wanted to create a script that could examine local OneDrive files and extract their attributes, particularly shedding light on their FOD states. After some exploration, I stumbled upon a reference script from Tristan Tyson’s tech blog that seemed promising.

The initial script was straightforward:

1
2
3
4
get-childitem | 
ForEach-Object {
    write-host "$_ :" $_.Attributes
}

This script looped through files and displayed their attributes. However, to adapt it for my needs, I needed to focus on OneDrive’s specific attributes.

The Reference Table and Adjustments

The reference script came with a table that associated attribute values with file states:

File StateAttribute
Cloud-Only5248544
Always available525344
Locally AvailableReparsePoint

However, during my testing phase, I realized that the provided values didn’t correctly align with OneDrive’s attributes on my machine.

I desperately scoured the archives in search of any mention of the elusive attributes but alas, I came up short in this endeavor. So, I rolled up my sleeves and manually tested different sync states to identify the correct attribute values and their corresponding human-readable states.

For testing (and ultimately my end goal), I took the reference script and adjusted it to drill down to the current users OneDrive (Commercial) directory. This allowed me (with toggling sync states multiple times) to identify the different attributes assiociated with different states.

1
2
3
4
get-childitem $ENV:OneDriveCommercial -Force -File -Recurse | 
ForEach-Object {
    write-host "$_ :" $_.Attributes
}

Armed with the information gathered in the testing phase, I then constructed my own reference table.

File StateAttribute
Cloud-Only5248544
Always available525344
Downloading4724256
Uploading1049632
Locally AvailableArchive, ReparsePoint
Hidden System File1572902

There might be additional attributes I didn’t come across during my testing phase. I’ll keep exploring and updating the reference table here whenever I spot them. For now, I deemed the collected data sufficient to make sense of the attribute values in a way that humans can understand.

Cracking the Code: The Finished Script

After traveling so far on this quest, the elements of the script emerged, ready to unveil the OneDrive attributes we sought. To that end, I focused on generating clear and comprehensible results into a table format, leveraging a switch statement and PSCustomObject to complete this final task.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$OneDrivePath = $ENV:OneDriveCommercial

$filesInfo = Get-ChildItem $OneDrivePath -Force -File -Recurse | ForEach-Object {
    $fileName = $_.Name
    $attributes = $_.Attributes

    $state = switch ($attributes) {
        5248544 { "Cloud-Only" }
        525344 { "Always Available" }
        4724256 { "Downloading" }
        1049632 { "Uploading" }
        1572902 { "Hidden System File" }
        "Archive, ReparsePoint" { "Locally Available" }
        default { "Unknown" }
    }

    [PSCustomObject]@{
        FileName = $fileName
        State    = $state
    }
}

$filesInfo | Format-Table -AutoSize -Wrap

The script was then tested for its efficiency at gathering the needed information.

With this final script, I was able to successfully list the OneDrive file attributes as desired. The script translates OneDrive file attribute values into human-readable states, making it easier than ever to understand the synchronization status of each file.

Final Thoughts and Further Explorations

In this exhilarating journey, we ventured into the realm of PowerShell to reveal the attributes of local OneDrive files. Armed with a script that interprets File On Demand states, we’ve unlocked a deeper understanding of file synchronization. As we continue our IT expeditions, let’s remain curious and ever-ready to explore the intricacies of our digital world.

Next time: We will look at setting the sync state of OneDrive files to “Online-Only” leveraging their file attributes to do so.

Automate and script away - Happy Shellcode Saturday!

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy