Professional Documents
Culture Documents
POWERSHELL SCRIPTING
Name Length
---- ------
tmp1.txt 82920
tmp2.txt 114000
tmp3.txt 114000
Pipe and Sort
The list is sorted alphabetically
◦ according to the process name.
You can sort WS field
◦ According to the memory usage
To do this the user
◦ “pipes” the output of ps
◦ to a second Cmdlet “sort-object”
ps | sort-object WS –descending
Sorting wrt Memory Usage
Filtering & Splitting the Commands
User can filter that
◦ the “powershell” not to be seen in output
Add a filter
piped Cmdlets can be
◦ split over multiple lines:
ps | where-object –FilterScript
{$_.processname -ne "powershell"}
| sort-object WS –descending
Using pipelines
• Most PowerShell cmdlets are designed to
support pipelines. In most cases, you
can pipe the results of a Get cmdlet to another
cmdlet of the same noun. For example, you can
pipe the output of the Get-Service cmdlet to
the Start-Service or Stop-Service cmdlets.
• Get-Service wmi | Start-Service
• This example pipeline starts the WMI service on
the computer
• For another example, you can pipe the output
of Get-Item or Get-ChildItem within the
PowerShell registry provider to the New-
ItemProperty cmdlet. This example adds a new
registry entry, NoOfEmployees, with a value
of 8124, to the MyCompany registry key.
• Get-Item -Path HKLM:\Software\MyCompany |
New-ItemProperty -Name NoOfEmployees -
Value 8124
• Many of the utility cmdlets, such as Get-
Member, Where-Object, Sort-Object, Group-
Object, and Measure-Object are used almost
exclusively in pipelines. You can pipe any
object type to these cmdlets. This example
shows how to sort all the processes on the
computer by the number of open handles in
each process.
• Get-Process | Sort-Object -Property handles
• You can pipe objects to the formatting, export,
and output cmdlets, such as Format-
List, Format-Table, Export-Clixml, Export-CSV,
and Out-File.
• This example shows how to use the Format-
List cmdlet to display a list of properties for a
process object.
• Get-Process winlogon | Format-List -Property *
Methods of accepting pipeline input
Cmdlets parameters can accept pipeline input in one of two
different ways:
• ByValue: The parameter accepts values that match the
expected .NET type or that can be converted to that type.
– For example, the Name parameter of Start-Service accepts pipeline
input by value. It can accept string objects or objects that can be
converted to strings.
• ByPropertyName: The parameter accepts input only when the
input object has a property of the same name as the parameter.
– For example, the Name parameter of Start-Service can accept objects
that have a Name property. To list the properties of an object, pipe it
to Get-Member.
Filtered
To concatenation symbol
The concatenation symbol (>)
◦ will send the result output file
◦ after the information has been converted to HTML
◦ so that it can then be viewed in a web browser:
ps |
where-object -FilterScript
{$_.processname -ne "powershell"} |
sort-object WS –descending |
convertto-html -property Name,
Displaying in Browser
Find all the processes on a computer that
started today
Get-Process | Where {$_.starttime -ge
[datetime]::today}
Find the processes that use more than 1000
MB of memory and kill them
get-process | where-object { $_.WS -gt
1000MB } | stop-process -whatif
Calculate the number of bytes in the files in a
directory
get-childitem | measure-object -property
length -sum
Defining Variables
PS C:\> $a = 5
PS C:\> $a
5
PS C:\>
Determine Variable
PS C:\> $a.GetType()
Piped Commands
dir | sort LastWriteTime | more
PS C:\WINDOWS> $a = dir | sort
LastWriteTime
PS C:\WINDOWS> $a[0]
PS C:\WINDOWS> $a[1]
Variables
• Short description
– Describes how variables store values that can be used in PowerShell.
• Long description
– You can store all types of values in PowerShell variables. For
example, store the results of commands, and store elements that are
used in commands and expressions, such as names, paths, settings,
and values.
– A variable is a unit of memory in which values are stored. In
PowerShell, variables are represented by text strings that begin with
a dollar sign ($), such as $a, $process, or $my_var.
types of variables in PowerShell
• User-created variables: User-created variables are created and maintained
by the user.
– By default, the variables that you create at the PowerShell command line exist only while
the PowerShell window is open. When the PowerShell windows is closed, the variables
are deleted. To save a variable, add it to your PowerShell profile.
• Automatic variables: Automatic variables store the state of PowerShell.
These variables are created by PowerShell, and PowerShell changes their
values as required to maintain their accuracy.
– For example, the $PSHOME variable stores the path to the PowerShell installation
directory. For more information, a list, and a description of the automatic variables, see
about_Automatic_Variables.
• Preference variables: Preference variables store user preferences for
PowerShell. These variables are created by PowerShell and are populated
with default values. Users can change the values of these variables.
– For example, the $MaximumHistoryCount variable determines the maximum number of
entries in the session history.
Working with variables
• To create a new variable, use an assignment statement
to assign a value to the variable. You don't have to
declare the variable before using it. The default value
of all variables is $null.
• To get a list of all the variables in your PowerShell
session, type Get-Variable. The variable names are
displayed without the preceding dollar ($) sign that is
used to reference variables.
• $MyVariable = 1, 2, 3
• $Path = "C:\Windows\System32"
• Variables are useful for storing the results of
commands.
– $Processes = Get-Process
– $Today = (Get-Date).DateTime
To display the value of a variable, type the variable
name, preceded by a dollar sign ($).
$MyVariable
$Today
• To delete the value of a variable, use the Clear-Variable cmdlet or
change the value to $null.
• $MyVariable = $null
• To delete the variable, use Remove-Variable or Remove-Item.
• $a = $b = $c = 0
• The next example assigns multiple values to multiple
variables.
Namespace Description
Alias: Aliases defined in the current
scope