Our mission is straightforward, to create a new-Item -type directory.
My learning agenda for PowerShell is as follows: 1) To appreciate
-type (-ItemType) and -path parameters. 2) To interrogate the capabilities of an object with Get-Member.
Example 1 - Find all the Dlls
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 Points
Note 1: Beginning a script with cls 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. See more on the $_
variable.
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.
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.
cls # PowerShell script to list the DLL files under the 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
Note 5: As usual with my -path examples, I
introduce a variable mainly to remind you to change its value if you
want the script to work on your machine!
Note 6: The -itemType parameter can be abbreviated
to plain -Type. Also "Directory" does not have to be in quotes.
Note 7: If the script succeeds PowerShell gives you
a summary of what the script achieved, however, I still like to open
Windows Explorer to checkout my new folder.
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.