Go crazy with Offline Files and PowerShell

Posted by

A customer asked me to write some example scripts for unpinning folders and files in their Offlines Cache and here are my examples. Bare with me the scripts are just examples and includes no error handling and only had time to test the scripts on a fully patched Windows 7 SP1 machine, but it should work on later OSes but with no guarantee!

If a user pinned some folders/files themselves by right clicking “Always available offline” try these commands

$path = “\\server\Share\Folder”
$objWMI = [wmiclass]”\\.\root\cimv2:win32_offlinefilescache”
$objWMI.UnPin($path, 0x00001820, $true)

If the folders/files are pinned by the system you need to figure out how the folders/files where pinned, and you can do that by testing these lines

$objWMI.UnPin($path, 0x00001840, $true)

$objWMI.UnPin($path, 0x00001880, $true)

You may ask where I got these numbers from, its from MSDN, right here: http://msdn.microsoft.com/en-us/library/bb309192(v=vs.85).aspx

Flag Value Meaning
OfflineFilesPinControlFlagForUser

0x00000020
Unpins the items for the calling user. This is the flag typically set for a caller of this function. It is important to note that Offline Files does not support a true per-user notion of pinning. When an item is unpinned for a user, it is pinned for all users of that machine. An item that is pinned with this flag can be unpinned by any user who has access to that file. The ability to access that pinned file depends on the user’s access rights to that file computed while online.
OfflineFilesPinControlFlagForUser_Policy

0x00000040
Unpins the items for per-user policy. This differs from the OfflineFilesPinControlFlagForUser flag in that this flag cannot be modified by the user through the Offline Files user interface. Internally, Offline Files sets this flag when items are unpinned by its Group Policy extension.
OfflineFilesPinControlFlagForAll

0x00000080
Unpins the items for all users of the local machine.

If you need to pin a folder/file for a user try this

$path = “\\server\Share\Folder”
$objWMI = [wmiclass]”\\.\root\cimv2:win32_offlinefilescache”
$objWMI.Pin($path, 0x00001221, $true)

And more information about the pinning command here: http://msdn.microsoft.com/en-us/library/bb309186(v=vs.85).aspx

 

Want to delete some folders or files from the Offline Files cache? OK lets go with these PowerShell commands

$path = “\\server\Share\Folder”
$objWMI = [wmiclass]”\\.\root\cimv2:win32_offlinefilescache”
$objWMI.DeleteItems($path, 0x0)

There parameters here a bit more interesting so don’t forget to have a look what it means, you may want to do something different. Here the MSDN link: http://msdn.microsoft.com/en-us/library/bb309183(v=vs.85).aspx

Flag Value Meaning
OfflineFilesDeleteFlagNoAutoCached

0x00000001
Do not delete automatically cached items. The default behavior is to delete automatically cached items.
OfflineFilesDeleteFlagNoPinned

0x00000002
Do not delete pinned items. The default behavior is to delete pinned items.
OfflineFilesDeleteFlagDelModified

0x00000004
Delete files even if files are locally modified in the cache. The default behavior is to not delete files with unsynchronized local changes.
OfflineFilesDeleteFlagAdmin

0x80000000
Allows administrators to enumerate and delete all files regardless of access rights. If this flag is set and the caller is not an administrator, the function fails.

 

If you want to combine two values just do a bit add an example:

0x00000002 + 0x00000004 + 0x80000000 = 0x80000006

0x00000002
0x00000004
+0x80000000
=0x80000006

That was all for this time! Good luck with your Offline Files and PowerShell Scripting!

One comment

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.