Hey, Scripting Guy! Question

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?

-- AW

Hey, Scripting Guy! Answer

Hello AW,

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.

ListItemsInTheUserProfileFolder.vbs

Const USER_PROFILE = &H28&

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(USER_PROFILE)
Set objFolderItem = objFolder.Self
Wscript.Echo objFolderItem.Path

Set colItems = objFolder.Items
For Each objItem in colItems
    Wscript.Echo objItem.Name
Next

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

Hexadecimal value

ALTSTARTUP

0x1d

APPDATA

0x1a

BITBUCKET

0xa

COMMONALTSTARTUP

0x1e

COMMONAPPDATA

0x23

COMMONDESKTOPDIR

0x19

COMMONFAVORITES

0x1f

COMMONPROGRAMS

0x17

COMMONSTARTMENU

0x16

COMMONSTARTUP

0x18

CONTROLS

0x3

COOKIES

0x21

DESKTOP

0x0

DESKTOPDIRECTORY

0x10

DRIVES

0x11

FAVORITES

0x6

FONTS

0x14

HISTORY

0x22

INTERNETCACHE

0x20

LOCALAPPDATA

0x1c

MYPICTURES

0x27

NETHOOD

0x13

NETWORK

0x12

PERSONAL

0x5

PRINTERS

0x4

PRINTHOOD

0x1b

PROFILE

0x28

PROGRAMFILES

0x26

PROGRAMFILESx86

0x30

PROGRAMS

0x2

RECENT

0x8

SENDTO

0x9

STARTMENU

0xb

STARTUP

0x7

SYSTEM

0x25

SYSTEMx86

0x29

TEMPLATES

0x15

WINDOWS

0x24

 

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.Self
Wscript.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.Name
Next

To translate this script into Windows PowerShell, you follow exactly the same logic. The ListItemsInTheUserProfileFolder.ps1 script is seen here.

ListItemsInTheUserProfileFolder.ps1

$User_Profile = 40

$objShell = new-object -comobject shell.application

$objFolder = $objShell.NameSpace($user_Profile)

$objFolderItem = $objFolder.self

$objFolderItem.path

$colItems = $objFolder.items()

ForEach ($objItem in $colItems)

{

 $objItem.name

}

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:

$User_Profile = 40

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:

$objShell = new-object -comobject shell.application

We then use the NameSpace method to return a folder object that points to the special folder. This is seen here:

$objFolder = $objShell.NameSpace($user_Profile)

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:

$objFolderItem = $objFolder.self

The path property of the folder object is used to display the location of the profile folder:

$objFolderItem.path

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:

$colItems = $objFolder.items()

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:

ForEach ($objItem in $colItems)

{

 $objItem.name

}

When the script is run, this is displayed:

Image of what is displayed when script is run

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 scripter@microsoft.com.

Ed Wilson and Craig Liebendorfer, Scripting Guys