Open table as spreadsheet
|
To |
Do This |
|---|---|
|
Produce a listing of all
|
Use the Set-Location cmdlet to change location to the variable PSDrive, then use the Get-ChildItem cmdlet |
|
Obtain a listing of all the aliases |
Use the
Set-Location
cmdlet to change location to the alias PSDrive, then use the
Get-ChildItem
cmdlet to produce a listing of aliases. Pipeline the resulting object into the
Where-Object
cmdlet and filter on the
|
|
Delete a directory that is empty |
Use the Remove-Item cmdlet and supply the name of the directory |
|
Delete a directory that contains other items |
Use the Remove-Item cmdlet and supply the name of the directory and specify the recurse argument |
|
Create a new text file |
Use the
|
|
Obtain a listing of registry keys from a registry hive |
Use the Get-ChildItem cmdlet and specify the appropriate PSDrive name for the -path argument. Complete the path with the appropriate registry path. Example: gci -path HKLM:\software |
|
Obtain a listing of all functions on the system |
Use the Get-ChildItem cmdlet and supply the PSDrive name of function:\ to the path argument. Example: gci -path function:\ |
Download CD Content
After completing this chapter, you will be able to:
Understand the reasons for writing Windows PowerShell scripts
Make the configuration changes required to run Windows PowerShell scripts
Understand how to run Windows PowerShell scripts
Understand how to break lines
Understand the use of
Create objects in a Windows PowerShell script
Call
With the ability to perform so many actions from inside Windows PowerShell in an interactive fashion, you may
Perhaps the number one reason to write a Windows PowerShell script is to address recurring needs. As an example, consider the activity of producing a directory listing. The simple
Get-
ChildItem
cmdlet does a good job, but after you decide to
Get-ChildItem c:\fso Where-Object {$_.Length -gt 1000} Sort-Object -Property
name
Even using
Tab Completion
, the previous command requires a bit of typing. One way to shorten it would be to create a
DirectoryListWithArguments.ps1
script is shown here:
DirectoryListWithArguments.ps1
foreach ($i in $args) {Get-ChildItem $i Where-Object {$_.length -gt 1000} Sort-Object -property name}
The
DirectoryListWithArguments.ps1
script takes a single, unnamed argument that allows the script to be modified when it is run. This makes the script much easier to work with and adds flexibility.
An additional reason that network administrators write Windows PowerShell scripts is to run the script as a scheduled task. In the Windows world, there are multiple task scheduler engines. Using the WIN32_ScheduledJob Windows Management Instrumentation (WMI) class, you can create, monitor, and delete scheduled jobs. This WMI class has been available since the Windows NT 4 days. On Windows XP and Windows Server 2003, the
The
ListProcessesSortResults.ps1
script is a script that a network administrator may want to schedule to run several times a day. It produces a list of currently running processes and
ListProcessesSortResults.ps1
$args = "localhost","loopback","127.0.0.1" foreach ($i in $args) {$strFile = "c:\mytest\"+ $i +"Processes.txt" Write-Host "Testing" $i "
please
wait ..."; Get-WmiObject -
computername
$i -class win32_process Select-Object name, processID, Priority, ThreadCount, PageFaults, PageFileUsage Where-Object {!$_.processID -eq 0} Sort-Object -property name Format-Table Out-File $strFile}
One other reason for writing Windows PowerShell scripts is that it makes it easy to store and share both the “secret commands” and the ideas behind the scripts. For example, suppose you develop a script that will connect remotely to workstations on your network and search for user accounts that do not require a password. Obviously, an account without a password is a security risk! After some searching around, you discover the WIN32_UserAccount WMI class and develop a script that
AccountsWithNoRequiredPassword.ps1
, which is seen here.
AccountsWithNoRequiredPassword.ps1
$args = "localhost" foreach ($i in $args) {Write-Host "Connecting to" $i "please wait ..."; Get-WmiObject -computername $i -class win32_UserAccount Select-Object Name, Disabled, PasswordRequired, SID, SIDType Where-Object {$_.PasswordRequired -eq 0} Sort-Object -property name Write-Host}