Prof. Powershell

Aren't You Special!

When two worlds collide -- that is, the VBscript and PowerShell worlds -- it can be a beautiful thing. Here's what I mean.

As much as I love working with PowerShell, sometimes I miss the old ways. Or I find, believe it or not, that VBScript offers an easier solution than PowerShell. For example, getting access to special folders like Favorites, Desktop or MyDocuments is much easier using the Wscript.Shell object from VBScript. Fortunately we can combine the best of both worlds.

First we need a shell object:

PS C:\> $wshell=New-Object -ComObject "wscript.shell"
PS C:\> $wshell

SpecialFolders     CurrentDirectory
--------------     ----------------
System.__ComObject C:\Users\Jeff

Pipe $wshell to Get-Member and you’ll see your favorite methods like PopUp. But we want to see special folders:

PS C:\> $wshell.SpecialFolders
C:\Users\Public\Desktop
C:\ProgramData\Microsoft\Windows\Start Menu
C:\ProgramData\Microsoft\Windows\Start Menu\Programs
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
C:\Users\Jeff\Desktop
C:\Users\Jeff\AppData\Roaming
C:\Users\Jeff\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
C:\Users\Jeff\AppData\Roaming\Microsoft\Windows\Templates
C:\Windows\Fonts
C:\Users\Jeff\AppData\Roaming\Microsoft\Windows\Network Shortcuts
C:\Users\Jeff\Desktop
C:\Users\Jeff\AppData\Roaming\Microsoft\Windows\Start Menu
C:\Users\Jeff\AppData\Roaming\Microsoft\Windows\SendTo
C:\Users\Jeff\AppData\Roaming\Microsoft\Windows\Recent
C:\Users\Jeff\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
C:\Users\Jeff\Favorites
C:\Users\Jeff\Documents
C:\Users\Jeff\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

Those are all the values, but you are more likely to reference them by name. For example, here are the paths to a few special folders:

PS C:\> $wshell.SpecialFolders.item("desktop")
C:\Users\Jeff\Desktop
PS C:\> $wshell.SpecialFolders.item("favorites")
C:\Users\Jeff\Favorites

Refer back to your VBScript reference material to discover the names of all the special folders. But once I have these I can incorporate them into my regular PowerShell expression. I like a clean desktop free of icon clutter:

PS C:\> dir $wshell.specialfolders.item("desktop") | del -whatif
What if: Performing operation "Remove File" on Target "C:\Users\Jeff\Desktop\Google Chrome.lnk".

I might need to back up my Favorites:

PS C:\> dir $wshell.specialfolders.item("favorites") -recurse |
>> copy -Destination \\file01\backup\favorites -passthru

Or I might want to see how much space my Application Data folder is consuming:

PS C:\> dir $wshell.specialfolders.item("AppData") -Recurse |
>> measure-object -Property length -sum
>>

Count    : 436
Average  :
Sum      : 16604129
Maximum  :
Minimum  :
Property : length

I know that some of these folders can be accessed via environmental variables. But this approach is not especially difficult and fits in nicely to traditional PowerShell administration.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular