Weekend Scripter: Working with Windows Libraries

Doctor Scripto

Summary: Microsoft PFE Chris Wu writes a guest blog article about using Windows PowerShell to work with Windows Libraries.

Microsoft Scripting Guy, Ed Wilson, is here. Today, we welcome guest blogger and Microsoft PFE Chris Wu back to the keyboard. Read more about Chris and his previous guest blogs here.  The keyboard is yours, Chris.

When engaged in Windows 7 deployment projects, one question I have been asked is how to manage Libraries. A Windows Library provides a way to organize and access files in an aggregated view regardless of where they are physically stored. Out-of-box four libraries are created: Documents, Music, Pictures, and Videos—each of them combines the corresponding Library folders under user profile and public profile.

Libraries are the default places where many applications search files from—Windows Media Player and many Windows Store applications on Windows 8 are some examples. In an enterprise environment, it may be a good idea to include a team-wide document folder located on a file share server other than redirecting the user’s own My Documents folder. Unfortunately, Library settings are saved in Library Description (*.library-ms) files and no group policy is available to control them, meaning deploying Libraries requires copying customized files, which can be a troublesome process.

Image of Libraries

Here, again, Windows PowerShell comes to the rescue.

Windows Shell API makes programmatically configuring Libraries possible, and the introduction of Windows API Code Pack for Microsoft .NET Framework enables a .NET approach that Windows PowerShell can conveniently leverage. For Libraries-related operations, two precompiled DLL from Windows API Code Pack are required. Simply download the current release and extract Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll (both can be found in the binaries folder) to some local folder (say c:\tools).

Then, we would be able to achieve the following common tasks:

  • Enumerate existing Libraries on the computer.

Add-type -path Microsoft.WindowsAPICodePack.Shell.dll

[Microsoft.WindowsAPICodePack.Shell.KnownFolders]::Libraries | Select-Object Name,ParsingName

                    Image of command output

  • Query settings of a Library. The second parameter of the Load method indicates whether to load the Library in read-only mode. Since changes will be made in the following examples, $false is used here.

$doc = [Microsoft.WindowsAPICodePack.Shell.ShellLibrary]::Load(“Documents”, $false)

Select-Object –InputObject $doc –Property Name, Count, DefaultSaveFolder, LibraryType

                     Image of command output

Note that I didn’t pipe the returned ShellLibrary object ($doc) to Select-Object as we usually do. Remember a Library is a collection of folders, so we wouldn’t be able to see its properties if we passed it to pipeline. To read the information of individual folder, however, we will use pipeline:

$doc | Select-Object Name, Path

                     Image of command output

  • Add a folder to a Library.

$doc.Add(“c:\docs”)

                    Image of command output

  • Set default save location (works only when the target folder is already included in the Library).

$doc.DefaultSaveFolder “c:\docs”

                    Image of command output

  • Remove a folder from a Library, and close the ShellLibrary object.

 $doc.Remove(“c:\docs”)

 $doc.Close()

                     Image of command output

  • Create a new Library. A Library is basically a self-described .library-ms file, which, when saved in “$env:appdata\Microsoft\Windows\Libraries” folder, will be discovered and displayed automatically by Windows Explorer. Below we will create a new Library called “PowerShell” and add a local script repository c:\ps (although it’s empty at the moment):

 $ps = New-Object Microsoft.WindowsAPICodePack.Shell.ShellLibrary –Argument “PowerShell”,$true

 $ps.Add(“c:\ps”)

 $ps.Close()

                      Image of command output

Cool! A new PowerShell Library is created. Like many readers of this blog, I have a large collection of scripts for different purposes: some for work, which are in a folder synchronized through SkyDrive Pro, and some others for personal stuff, which are saved in a different folder created by SkyDrive.

Now with a PowerShell Library, a single-click in Windows Explorer will list all those scripts, and all I have to do is to add the following script snippet in a preparation script that runs once on each of my new machines.

 Add-type -path Microsoft.WindowsAPICodePack.Shell.dll

 # For each Library, add SkyDrive and SkyDrive Pro folders (assuming default locations)

 [Microsoft.WindowsAPICodePack.Shell.KnownFolders]::Libraries | % {

   $library = [Microsoft.WindowsAPICodePack.Shell.ShellLibrary]::Load(“$($_.Name)”, $false)

   $library.Add(“$($env:userprofile)\Skydrive\$($_.Name)”)

   $library.Add(“$($env:userprofile)\SkydrivePro\$($_.Name)”)

   $library.Close()

 }

 

 # Create a PowerShell Library and include corresponding folders

 $ps = New-Object Microsoft.WindowsAPICodePack.Shell.ShellLibrary –Argument “PowerShell”,$true

 $ps.Add(“$($env:userprofile)\Skydrive\PowerShell”)

 $ps.Add(“$($env:userprofile)\SkydrivePro\PowerShell”)

 $ps.Close()

 

That’s all I have to share in this post. For more information about the Windows API Code Pack and the ShellLibrary class, refer to the document that comes with the binaries.

Thank you, Chris. I really appreciate you and all our other guest bloggers that take the time to share their knowledge.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon