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.
- By Jeffery Hicks
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
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
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")
PS C:\> $wshell.SpecialFolders.item("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
Sum : 16604129
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.
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.