Module 2: PowerShell Command-lets, PSProviders and PSDrives
Finding command let:
Command-let=Verb+Noun(Get,Set,New,Remove,Clear,Delete,Start,Stop;Item,Service,
Process,EventLog etc..)
Get-Command lists all available commands
Module is binding of group of commandlets
Get-Command –Module <Module_Name> gives group of command-lets available in given
module
Each command-let takes it’s own parameters along with common parameters
Output of command-let is an object
Output of command-let can be piped to any other command-let
Powershell Help:
• Why do we need Help system?
• Don’t memorize – just Discover!!
• Thousands of commands – all have help
• Scripting resources and information
• Advanced PowerShell configuration information
• Update-help –force //To update the help
• Save-help <path> //To store help in a file
• Update-help –pspath <path> // To Update help from a local/shared disk
• Update-help required Internet Connection
• It needs to be executed in Elevated mode
Apple Software Solutions Page 1
Discoverability with Help:
➢ Get-Help versus Help and Man
➢ Help <cmdlet>
➢ Help *partial*
➢ Help <verb/noun>
➢ Help <cmdlet> -Full
➢ Help <cmdlet> -Online
➢ Help <cmdlet> -ShowWindow
➢ Get-Help About_*
Get-Childitem:
Get the list of items available in file system/certificate store / registry.
Eg:
Get-Childitem C:\windows
Get-Childitem c:\windows –filter “*.txt”
Get-Childitem c:\windows –recuse
Where-Object:
• Used for filtering objects in the pipeline
Aliases: where, ?
• Traditional syntax uses a script block and $_
PS C:\> get-service m* | where {$_.status –eq 'running'}
• New syntax can be simpler
PS C:\> get-service m* | where status –eq 'running
Apple Software Solutions Page 2
Select-Object:
• Select just what you need
Aliases: Select, slo
• Select object properties
Filter properties with –Exclude or –Include
Expand nested object properties
Writes a custom object to the pipeline
• Select a subset of objects
First or Last X number of objects
Unique objects
Skip first X number of objects (starting at 1)
• Many of these parameters can be combined
• Selecting is not the same as formatting
• PS C:\> get-process | select id,name,workingset,path –first 5
Sort-Object:
• Sort objects on property value, usually numeric
Alias: sort
• Specify a comma separated list of property names
Can sort on a custom property
• Default order is ascending
Or use –Descending
Can only sort multiple properties in one direction unless you use custom properties
• Not case-sensitive by default
• Get objects with a uniquevalue for property name
• •PS C:\> get-process | sort WorkingSet –descending | select –first 10
– Remoting tabs
Apple Software Solutions Page 3
Group-Object:
• Organize objects into groups based on a property
Alias: group
• Creates a new object
• Microsoft.PowerShell.Commands.GroupInfo
Count, Name, Group (array of grouped objects)
Use –NoElement to only get Count and Name
• Can create a hash table instead
• Can group on a custom property
• PS C:\> get-process| group company -noelement | sort
count,@{Expression="name";Descending=$False} –descending
Measure-Object:
• Alias: measure
• Writes a new object to the pipeline
• Microsoft.PowerShell.Commands.GenericMeasureInfo
• Perform quick measurements based on a numeric property
– Always gets Count
– Sum, Minimum, Maximum, Average
• Perform quick measurements based on words and characters
– Character, Line, Word
• PS C:\> dir c:\scripts –file | measure length –sum -average
Get-content:
• Read text file contents
– Aliases: cat, type, gc
• Improve performance for very large files with –ReadCount
• Get the “head” of a file (–TotalCount)
–parameter aliases: –head, –first
• Get the “tail” of a file (–Tail)
• Read complex text files (-Encoding, -Delimiter)
Apple Software Solutions Page 4
Aliases:
• Alias is alternate name or nick name of cmdlet/command element such as function,
script, file, or executable
• New-Alias cmdlet to create alias
• Alias cannot be created for cmdlet with parameters
• Built-In aliases – cd, chdir, ls, dir etc.
• Get-Command *Alias* gives all cmdlets related to aliases
• Get-Alias –Path Alias: gives all aliases available
• $Alias:<alias_Name> : To resolve alias
Parameters:
• Parameters are options to provide inputs to PowerShell commands
• The parameter name always preceded by hyphen (-)
• -<Parameter_Name> <Parameter_Value>
• Use Get-Help or Help to find more information about cmdlet parameters
• To find more information about parameters of script use Help <Pathof script file>
• Help About_Parameters to find more information about parameters
• Parameter attribute table includes detailed information about parameters
• Optional Parameters
• Default values of parameters would be used when no parameter value provide
• Script attribute Mandatory=$false makes parameter optional
• [Param Arg] indicates parameter is optional in full help section
• Most of the common parameters are optional
• ComputerName parameter is optional for most of the PowerShell cmd-lets
• Positional Parameters
• Parameter name is optional, Parameter value will be assigned based on its position
• Named parameters will be ignored to consider parameter value positions
• By default all function parameters are positional
• Assigns position numbers to parameters in the order of declaration
Apple Software Solutions Page 5
• To disable default assignment of positions, set PositionalBinding argument of
CmdletBinding attribute to $false
• Position values start from 0
• Common Parameters
• PowerShell includes many out of the box parameters
• Common parameters can use with any Cmdlet
• These might not have effect on all Cmdlets
• These can also available with advanced functions
• -debug
• -ErrorAction
• -Verbose
• -WhatIf
• -ErrorVariabl
• -OutVariable
• -Confirm
Formatting:
• PowerShell decides how to format objects at the end of the pipeline
• PowerShell examines the object type
• Finds the object in its .ps1xml configuration files
• PS C:\> dir $pshome\*.ps1xml
• PS C:\> help about_format
• PowerShell follows a set of rules
• Displays formatted result
• You can over-ride PowerShell’s formatting decisions
• Manually invoking the format cmdlets
• Creating your own type and format extensions
Apple Software Solutions Page 6
Format-Table:
Alias = ft
Output may be truncated
Specify properties to display
Create custom properties
Group data by property
Customize appearance
Wrap
Autosize
PS C:\> ps | sort Company | ft –group Company Handle,Name,WS,StartTime
Format-List:
Alias=fl
Ouput less likely to be truncated
Specify property to display
Create Custom properties
Group data by property
PS C:\> ps | sort Company | fl -group Company
Format-Wide:
Alias=fw
Good for simple, broad display
Output may be truncated
Specify property to display
Specify number of columns
Autosize
Group by property
PS C:\> gsv | where status –eq running | fw –col 4
Apple Software Solutions Page 7
Understanding Syntax:
PSProviders:
• Software that translates data storage to a PowerShell friendly format
• Typical hierarchical data structures
✓ FileSystem
✓ Registry
✓ Certificate Store
✓ Active Directory
✓ WSMan
✓ Environment
✓ IIS
• Additional providers may be added via Modules/Snap-Ins
• Get-PSProvider to view installed providers
• To get help Help About_Providers
Apple Software Solutions Page 8
PSDrives:
• Act like filesystem drives
• Not limited to letters
• Use Get-PSDrive to view available drives
• Many drives created by default
• Drives only persist for as long as your PowerShell session is running
Creating PSDrive:
• Use New-PSDrive
- Specify drive name
- Specify provider name
- Specify path
- Options : Credential, Persistence
• Drives not necessarily seen by Windows
• Add drive settings to your PowerShell profile script
Removing PSDrive:
• User defined drives end with session
• Use Remove-PSDrive to manually removal
• Can’t remove Windows or fixed drives
• Don’t try to remove your current drive
Working with Item/Item Property:
• Get-Item – Gets the item at specified location
• Get-ItemProperty – gets the properties of specified item
• Remove-Item – Deletes the item
• Set-Item – Change the value of item
• Clear-item – To clear content of an item
• Rename-Item/ItemProperty – Renames the Item/ItemProperty
• Copy-Item/ItemProperty – Copy Item/ItemProperty
• Move-Item/ItemProperty – Move Item/ItemProperty to different location
Apple Software Solutions Page 9