Learn about Windows PowerShell
Summary: Use Windows PowerShell to list files in folders and subfolders.
Hey, Scripting Guy! I am an old VBScript guy. I love using VBScript, and I have done so for nearly 15 years. One reason I love VBScript so much is that, to me, it is easy to use. It is also very powerful. But after attending TechEd the last several years, it appears that Microsoft is moving away from VBScript, and is embracing Windows PowerShell.
So, I am trying to learn Windows PowerShell. What I am doing is when I need to make a change to an existing VBScript script, I attempt to use Windows PowerShell to do the same task. I am having a bit of trouble listing files in folders and in subfolders. Can you give me a push in the right direction?
Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a cup of English Breakfast tea. I added a combination spoonful of spearmint leaves, peppermint leaves, licorice root, rose hips, hibiscus, and a cinnamon stick to my pot while I steeped it for four minutes. The tea is very refreshing. Speaking of refreshing, I believe you will find the way that Windows PowerShell handles files and folders a welcome change from the work you had to do in VBScript.
First let me say that I do not hate VBScript. I wrote three books about VBScript, and I had a lot of fun teaching VBScript classes all over the world when I was in MCS. I am not the type of person who wants to say that you should immediately change over VBScript to Windows PowerShell. The point of scripting—regardless of the language—is for automation. Scripting is a tool, a means to a destination, not the destination itself. Therefore, TJ, I believe that you are taking a logical approach to learning Windows PowerShell and preparing for the future of Windows automation.
But, nearly 10 years ago, we posted the following Hey, Scripting Guy! Blog: How Can I Get a List of All the Files in a Folder and Its Subfolders? The script, written in VBScript, was 22 lines long. It was a lot of work to understand, and for someone not really familiar with VBScript, it would be quite confusing. Here is the code—just for fun.
VBScript list files in folders and subfolders
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Scripts"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
There are at least three issues with this script.
One thing to keep in mind is that if you double-click this VBScript script, you potentially will receive thousands of popup message boxes like the one shown here:
Another issue is that whether I click OK, or I click the red “X” in the upper-right corner, the onslaught of popup dialog boxes keeps coming. The only way to retrieve control of the situation (other than rebooting my computer) is to open Task Manager, find the Windows-based script host process, and kill it. This is illustrated in the following image:
The third issue is a bit different. It involves a bit of usability because to change the destination of the script, I must manually edit the script. This involves opening the script in Notepad (the only editor we ship for VBScript), and changing the value of ObjStartFolder.
Now, each of these issues is solvable in that I can check to ensure the script is running in a command prompt via Cscript, and I can add command line input variables to the script. But both of these solutions are going to involve several lines of additional code.
When it comes to working with files and folders and recursing through a nested directory structure, it is almost an unfair competition between Windows PowerShell and VBScript. It is almost like the Windows PowerShell team deliberately made it easy to work with files and folders.
For example, suppose I have a series of nested folders in my music folder, such as the one shown in the following image. Each folder is a music group, each subfolder is an album, and inside each album are the individual music tracks.
To work with a specific folder, I use the Get-ChildItem cmdlet. This cmdlet has been around since Windows PowerShell 1.0, but in more recent versions of Windows PowerShell, it has gained a couple of additional useful switches. First, just list a specific folder:
Get-ChildItem -Path E:\music
This command lists all files and folders that are at the E:\music level. This command does not recurse through the entire structure. The command and a sample output are shown in the following image:
If I want to only see the folders at this level, I use the –Directory switch.
Note The –Directory, -File, -Attributes, -Hidden, and –System switches were added to Get-ChildItem cmdlet in Windows PowerShell 3.0. These switches are available via the FileSystem provider. For more information, see Use PowerShell to Find Dynamic Parameters.
This would be the command to see only the directories at the E:\Music level:
Get-ChildItem -Path E:\music –Directory
To see only the files at this level, I change it to use the –File switch:
Get-ChildItem -Path E:\music –File
To burrow down into a nested folder structure, I need to use the –Recurse switch. The difference is readily apparent. For example, the following command displays the folders in a specific folder:
Get-ChildItem -Path E:\music\Santana
The output from the command is shown here:
When I use the –Recurse switch, I can see the folders in addition to the files inside each of the folders. The command is shown here:
Get-ChildItem -Path E:\music\Santana –Recurse
In the following image, I see that at first the output is the same—it lists the folders. Then it takes each folder in turn, and displays the files from that folder. This continues until all the files in all the nested folders are displayed.
If I use the –File parameter, I do not get the initial folder list:
Get-ChildItem -Path E:\music\Santana -Recurse –File
The command and output from the command are shown here:
One of the really cool things that Windows PowerShell does is makes it easy to sort information. There is even a cmdlet named Sort-Object. For example, if I want to see which songs are the longest, I would use this command:
Get-ChildItem -Path E:\music\Santana -Recurse -File | sort length –Descending
TJ, that is all there is to using Windows PowerShell to list files in folders and subfolders. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Your http://www.equicorplegal.com">matter is very nice and work to cmd
So when I -recurse and want to act on the list of files returned by gci, how do I retrieve the directory of the file being acted on? All I can seem to get to is the file name, and not the path it is at.
@David. Have you tried "DirectoryName" - that would seem to be the way to get the directory part of the filename.FullnameBaseNameExtensionDirectoryname[io.fileinfo]'x' | gm