Learn about Windows PowerShell
Hey Scripting Guy! I am wondering about something. I was looking on the Script Center Script Gallery, and I came across a pretty cool script that lists items that are in the user's profile. It is in VBScript. Can you translate it to Windows PowerShell for me?
Microsoft Scripting Guy Ed Wilson here. It is early morning. I am listing to Luciano Pavarotti on my Zune as he sings Lunge da Lei from Verdi's La Traviata, and I’m sipping a cup of English Breakfast tea from my little blue tea pot the Scripting Wife bought me. The tea has a stick of real cinnamon in it, and I am munching on a freshly baked peach scone (peaches are in season in Charlotte, North Carolina, in the United States). Needless to say, I am in a good mood. Life is good. And I bought a new Microsoft keyboard the other day at the company store in Charlotte, so I am all set to answer your question.
I love our new Script Gallery—the search feature is cool, and the ability to filter scripts based upon the categories and scripting languages is sweet. The fact that you can directly upload scripts and not have to e-mail scripts to us and then wait for us to get around to publishing them is obviously another huge benefit. The rating, ranking, and discussion will turn out to be a big bonus as well. In short, as we used to say in Southern California, it rocks dude!
AW, you found the List Items in the User Profile Folder, which is seen here.
Const USER_PROFILE = &H28&Set objShell = CreateObject("Shell.Application")Set objFolder = objShell.NameSpace(USER_PROFILE)Set objFolderItem = objFolder.SelfWscript.Echo objFolderItem.PathSet colItems = objFolder.ItemsFor Each objItem in colItems Wscript.Echo objItem.NameNext
The ListItemsInTheUserProfileFolder.vbs script begins by creating a constant, USER_PROFILE, and assigning hexadecimal 28 to it. A listing of shell folder constants is seen in Table 1; they are also documented on MSDN.
Table 1 Shell special folder values
Special Folder name
The ListItemsInTheUserProfileFolder.vbs script next creates an instance of the Shell.Application object and stores the object in the variable objShell. The Shell.Application object is documented on MSDN and it provides many cool capabilities such as browse for folder and the ability to work with special folders. This is seen here:
Set objShell = CreateObject("Shell.Application")
After the Shell.Application object has been created, the NameSpace method is used to return a folder object that represents the profile folder”
Set objFolder = objShell.NameSpace(USER_PROFILE)
The self property is used to point the folder object to the current folder, and the path property is used to display the path to the profile folder. This is seen here:
Set objFolderItem = objFolder.SelfWscript.Echo objFolderItem.Path
The items method returns a collection of all the items in the folder.
Set colItems = objFolder.Items
The For Each Next construction is used to walk through the collection of items stored in the colItems variable:
For Each objItem in colItems Wscript.Echo objItem.NameNext
To translate this script into Windows PowerShell, you follow exactly the same logic. The ListItemsInTheUserProfileFolder.ps1 script is seen here.
$User_Profile = 40
$objShell = new-object -comobject shell.application
$objFolder = $objShell.NameSpace($user_Profile)
$objFolderItem = $objFolder.self
$colItems = $objFolder.items()
ForEach ($objItem in $colItems)
A variable $User_Profile is created and assigned the value of 40 decimal. This is the same as Hexadecimal 28 and is easier to read. A constant is not really needed here as we are not planning on modifying the value of the variable. This is seen here:
Next we create an instance of the shell.application object. Because it is a COM object, we use the comobject parameter with the New-Object cmdlet. We store the returned shell.application object in the variable $objShell. This is shown here:
We then use the NameSpace method to return a folder object that points to the special folder. This is seen here:
The self property is used to return a pointer to the special folder. The returned folder object is stored in the $objFolderItem variable. This is seen here:
The path property of the folder object is used to display the location of the profile folder:
Next, we obtain a collection of all the items in the folder by using the items method from the folder object. The resulting collection is stored in the $colItems variable. This is seen here:
The ForEach statement is used to walk through the collection of items in the $colItems variable. Inside the loop, the name property of each item is displayed:
When the script is run, this is displayed:
Well, AW, we hope you enjoyed translating the ListItemsInTheUserProfileFolder.vbs script into Windows PowerShell. Along the way, we saw how to use the shell.application object in Windows PowerShell, and we also learned a little about special folders.
We invite you to follow us on Twitter or Facebook so that you can keep up to date with the latest Script Center news. If you have a question, you can always ask it on the Official Scripting Guys Forum, or send us e-mail at firstname.lastname@example.org.
Ed Wilson and Craig Liebendorfer, Scripting Guys