PowerShell Script to List FilesThese examples explain how to get started
scripting files with PowerShell.
Because I dislike 'Hello World' examples, my scripts have a practical purpose, in this instance to get a listing of a particular type of file, in a particular directory tree.
♣
My mission is to list all files with a given extension in a particular
directory tree, then write the output to a file. Specifically, all dlls in the windows\windows32 folder. This simple task is much like typing the DOS command: dir \s. What would make my day is if you can alter my script to
search for files that interest you. My learning agenda for PowerShell is as follows: - To appreciate the rhythm of the
PowerShell language. Verb-noun pairs.
- To demonstrate how easy it is to introduce $variables
- To employ the flexibility of the pipe command (|) so that the output
of the first command becomes the input of a second command.
- To interrogate the capabilities of an object with Get-Member.
Pre-requisites
You need to have already installed a local copy of PowerShell. If necessary search the internet for 'Microsoft PowerShell download'. Note PowerShell RC2 is much changed from its
predecessor, release candidate 1. Instructions - Save the following script to a text file with a .ps1 extension, for example: C: \Script\dll.ps1.
- Launch PowerShell and navigate to the folder where you saved the .ps1 file. Incidentally, the Dos cd command works fine in PowerShell.
- To call for your script file, type at the command line PS> .\dll.ps1.
Note 1. The rhythm of the command is: dot slash filename. Note 2. You don't have to type PS>
# PowerShell script to list the DLL files under the system32 folder
Clear-Host $Dir = Get-Childitem C:\windows\system32 -recurse # $Dir |Get-Member $List = $Dir | where {$_.extension -eq ".dll"} $List |
format-Table name
Learning PointsNote 1: Beginning a script with
Clear-Host is one of my idiosyncrasies, it simply means clear the screen of any previous output (just as it does in DOS). The hash symbol # means a remark, or do
not process this line. Note 2:
$Dir = Get-Childitem C:\windows\system32 -recurse This command sets the variable $Dir to the path of the files that we seek. You have probably guessed the purpose of the -recurse
switch, to drill down to the sub-folders. Get-childitem is often abbreviated to its alias, gci. Note 3: $List = $Dir | where {$_.extension -eq ".dll"} $List is another variable whose
purpose is to filter the output, as a result we get only files with .dll extension. Pay particular attention to the construction $_. which means, in this pipeline. Also observe that instead of the
equals sign, PowerShell needs -eq. Note 4: One of PowerShell's features is the pipe symbol (|). Most PowerShell scripts contain at least one pipe to control, or filter the output of the
main command.
Note Out-GridView: PowerShell v 2.0 introduces a
new cmdlet to control data display.
See more on how to pipe the results
into out-GridView.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, SolarWinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
Download your free copy of WMI Monitor
The purpose of this script is to list the properties of the file object controlled by the $dir variable. When we display the list, how much information do we want? What file properties are
available? All is revealed by Get-Member. # PowerShell script to list DLL files under system32 folder $Dir = Get-Childitem C:\windows\system32 -recurse $Dir |Get-Member # $List = $Dir | where {$_.extension -eq ".dll"} # $List
|ft fullname |out-file C:\scripts\dll.txt # List | format-Table name
Learning PointsNote 1: This is the same script as example 1, all that has changed is the #. I have 'remmed' out the list and activated the line: $Dir |Get-Member.
Note 2: Get-Member lists the properties and methods of the PowerShell object $Dir. Once we have our list we can control the output specified by format-Table. See next example.
Seeing the files on screen is
fine, but with lists of files, it's often handy to generate a file containing your script's output. # PowerShell
Pipe To a File Clear-Host $Dir = Get-Childitem C:\windows\system32 -recurse # $Dir |Get-Member $List = $Dir | where {$_.extension -eq ".dll"} $List
|ft fullname |out-file C:\Scripts\dll.txt # List | format-Table name
Learning PointsNote 1: Here is the line that I activated by removing the #. $List |ft fullname |out-file c: \Scripts\dll.txt. What this does is create a file in the c: \Scripts folder. (Note it does not create the \Scripts folder itself, you must do that yourself.) In that dll.txt file are the names of all the files with a .dll extension. Note 2: ft,
meaning format-Table, is one of the built-in aliases which make the code shorter to write.
Guy Recommends: SolarWinds' Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
- Bulk-import new users into Active Directory.
- Seek and zap unwanted user accounts.
- Find inactive computers.
Download your FREE bulk import tool.
Challenge 1: Set the script to list a different extension, for example .com or .exe.
Challenge 2: Persuade the script to output the 'length' property of the files.
Challenge 3: Filter the object property with -MemberType, for example remove unwanted Methods leaving only 'Property': $Dir |Get-Member -MemberType property
PowerShell is a very efficient scripting language. In this example we use $variables and verb-noun
pairs to create a script which lists the files we seek. I hope you will tweak my examples to list the types of files that you need. Moreover, I hope that you can see how to adjust the variable,
as a result you can locate a directory tree of your choosing. As a bonus, Example 3 outputs the list of files, not to screen, but to a text file.
If you like this page then please share it with your friends
See more Microsoft PowerShell file tutorials:
• PowerShell Home •
Add-Content •
Get-Content •
Set-Content •
Test-Path •
-recurse
•
Files •
PowerShell Registry •
PowerShell ItemProperty •
Compare-object
•
Get-Credential •
Windows
PowerShell
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Download my ebook: Getting Started with PowerShell - only $9.25
You get 36 topics organized into these 3 sections: 1) Getting Started 2) Real-life tasks 3) Examples of Syntax.
In addition to the ebook, you get a PDF version of this Introduction to PowerShell ebook It runs to 120
pages of A4.
|