Cmdlets output objects, and can consume objects.
Objects have properties. Properties can be read-only, or modifiable.
Not all properties are shown by default when you view an object
Objects have methods, which may have parameters. Cmdlets are often associated with methods, but methods can be invoked manually too.
Get-Member lists all properties and methods of an object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
PS C:\> Get-Service | Get-Member TypeName: System.ServiceProcess.ServiceController Name MemberType Definition —— ————— ————— Name AliasProperty Name = ServiceName RequiredServices AliasProperty RequiredServices = ServicesDependedOn Disposed Event System.EventHandler Disposed(System.Object, System.EventArgs) Close Method void Close() Continue Method void Continue() CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType) Dispose Method void Dispose(), void IDisposable.Dispose() Equals Method bool Equals(System.Object obj) ExecuteCommand Method void ExecuteCommand(int command) GetHashCode Method int GetHashCode() GetLifetimeService Method System.Object GetLifetimeService() GetType Method type GetType() InitializeLifetimeService Method System.Object InitializeLifetimeService() Pause Method void Pause() Refresh Method void Refresh() Start Method void Start(), void Start(string[] args) Stop Method void Stop() WaitForStatus Method void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desi. CanPauseAndContinue Property bool CanPauseAndContinue {get;} CanShutdown Property bool CanShutdown {get;} CanStop Property bool CanStop {get;} Container Property System.ComponentModel.IContainer Container {get;} DependentServices Property System.ServiceProcess.ServiceController[] DependentServices {get;} DisplayName Property string DisplayName {get;set;} MachineName Property string MachineName {get;set;} ServiceHandle Property System.Runtime.InteropServices.SafeHandle ServiceHandle {get;} ServiceName Property string ServiceName {get;set;} ServicesDependedOn Property System.ServiceProcess.ServiceController[] ServicesDependedOn {get;} ServiceType Property System.ServiceProcess.ServiceType ServiceType {get;} Site Property System.ComponentModel.ISite Site {get;set;} StartType Property System.ServiceProcess.ServiceStartMode StartType {get;} Status Property System.ServiceProcess.ServiceControllerStatus Status {get;} ToString ScriptMethod System.Object ToString(); |
Can filter to just methods or properties etc,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
PS C:\> Get-Service |Get-Member -MemberType Method TypeName: System.ServiceProcess.ServiceController Name MemberType Definition —— ————— ————— Close Method void Close() Continue Method void Continue() CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType) Dispose Method void Dispose(), void IDisposable.Dispose() Equals Method bool Equals(System.Object obj) ExecuteCommand Method void ExecuteCommand(int command) GetHashCode Method int GetHashCode() GetLifetimeService Method System.Object GetLifetimeService() GetType Method type GetType() InitializeLifetimeService Method System.Object InitializeLifetimeService() Pause Method void Pause() Refresh Method void Refresh() Start Method void Start(), void Start(string[] args) Stop Method void Stop() WaitForStatus Method void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desired.. |
Select-Object works with any object.
Pipe an object into Select-Object to view it indifferent ways, -Property says which properties (columns) to show
1 |
PS C:\> Get–Service | Select–Object –Property Name, CanStop |
OR to get all properties can do this, but it will show in a non-table way:
1 |
Get–Service | Select * |
To list all the properties for the actual object and not the type.
Sorting
pipe through Sort
Select a property to sort on , and ascending or descending
1 |
get–service |sort Status |
Or
1 |
get–service | sort Status –Descending |
Sometimes the sort might appear to not work. This can be because the sort is not working on the text value, but an underlying numeric value. In this case, you need to convert the property to a text value and sort on that instead, e.g.
1 |
Get-MyObjectName | Sort {$_.MyPropertyName.ToString()} – Descending |
Filtering
To get a subset of results, use Where-Object (alias = Where), specifying the property and condition to filter on, e.g.:
1 2 3 4 5 6 7 8 9 |
PS C:\> Get-Service | Where-Object -Property Status -EQ Stopped Status Name DisplayName ——— —— —————– Stopped AJRouter AllJoyn Router Service Stopped ALG Application Layer Gateway Service Stopped AppIDSvc Application Identity Stopped AppMgmt Application Management Stopped AppReadiness App Readiness |
Operators are:
-eq
-lt
-gt
-ceq (case sensitive)
-like (wildcard match)
-notlike (wildcard match)
-match (regex)
The shorthand version is more widely used and more flexible when it comes to complicated expressions
$_ means the current object in the pipeline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
PS C:\> Get-Service | Where-Object {$_.Status -EQ ‘Stopped’ -and $_.Name -like ‘V*’} Status Name DisplayName ——— —— —————– Stopped vds Virtual Disk Stopped vmicguestinterface Hyper-V Guest Service Interface Stopped vmicheartbeat Hyper-V Heartbeat Service Stopped vmickvpexchange Hyper-V Data Exchange Service Stopped vmicrdv Hyper-V Remote Desktop Virtualizati... Stopped vmicshutdown Hyper-V Guest Shutdown Service Stopped vmictimesync Hyper-V Time Synchronization Service Stopped vmicvmsession Hyper-V PowerShell Direct Service Stopped vmicvss Hyper-V Volume Shadow Copy Requestor Stopped VSS Volume Shadow Copy Stopped VSStandardColle... Visual Studio Standard Collector Se... Stopped VSStandardColle... Visual Studio Standard Collector Se... |
Grouping
Objects can be grouped based on properties.
1 2 3 4 5 6 |
PS C:\> Get-Service | Group-Object -Property Status Count Name Group ——– —— ——– 116 Running {AdobeARMservice, Appinfo, A 134 Stopped {AJRouter, ALG, AppIDSvc, Ap |
This is now a GroupInfo Object, so if we wanted to get hold of the first running service from here, we would need to expand the group property to get the service objects in it, and then get the first one of those:
1 2 3 4 5 |
PS C:\> Get-Service | Group Status | Where-Object -Property Name -EQ Running | select -ExpandProperty group |select -first 1 Status Name DisplayName ——— —— —————– Running AdobeARMservice Adobe Acrobat Update Service |
Piping to Out-GridView pops out a GUI window with resizable columns etc