PowerShell is an effective command line interface to retrieve information about computer systems and services. Type in a command like get-service, and you receive a listing of all the services on a Windows system and their status.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <span class="hljs-string">PS </span>C:<span class="hljs-string">GitHub</span><span class="hljs-string">demos></span> <span class="hljs-built_in">get-service</span> <span class="hljs-string">Status </span> <span class="hljs-string">Name </span> <span class="hljs-string">DisplayName </span>------ ---- ----------- <span class="hljs-string">Running </span> <span class="hljs-string">AdobeARMservice </span> <span class="hljs-string">Adobe </span><span class="hljs-string">Acrobat </span><span class="hljs-string">Update </span><span class="hljs-string">Service </span><span class="hljs-string">Stopped </span> <span class="hljs-string">AJRouter </span> <span class="hljs-string">AllJoyn </span><span class="hljs-string">Router </span><span class="hljs-string">Service </span><span class="hljs-string">Stopped </span> <span class="hljs-string">ALG </span> <span class="hljs-string">Application </span><span class="hljs-string">Layer </span><span class="hljs-string">Gateway </span><span class="hljs-string">Service </span><span class="hljs-string">Stopped </span> <span class="hljs-string">AppIDSvc </span> <span class="hljs-string">Application </span><span class="hljs-string">Identity </span><span class="hljs-string">Running </span> <span class="hljs-string">Appinfo </span> <span class="hljs-string">Application </span><span class="hljs-string">Information </span><span class="hljs-string">Stopped </span> <span class="hljs-string">AppMgmt </span> <span class="hljs-string">Application </span><span class="hljs-string">Management </span><span class="hljs-string">Stopped </span> <span class="hljs-string">AppReadiness </span> <span class="hljs-string">App </span><span class="hljs-string">Readiness </span><span class="hljs-string">Stopped </span> <span class="hljs-string">AppVClient </span> <span class="hljs-string">Microsoft </span><span class="hljs-string">App-V </span><span class="hljs-string">Client </span><span class="hljs-string">Stopped </span> <span class="hljs-string">AppXSvc </span> <span class="hljs-string">AppX </span><span class="hljs-string">Deployment </span><span class="hljs-string">Service </span>(<span class="hljs-string">AppXSVC)</span> <span class="hljs-string">Stopped </span> <span class="hljs-string">AssignedAccessM.</span>.. <span class="hljs-string">AssignedAccessManager </span><span class="hljs-string">Service </span><span class="hljs-string">Running </span> <span class="hljs-string">AudioEndpointBu.</span>.. <span class="hljs-string">Windows </span><span class="hljs-string">Audio </span><span class="hljs-string">Endpoint </span><span class="hljs-string">Builder </span><span class="hljs-string">Running </span> <span class="hljs-string">Audiosrv </span> <span class="hljs-string">Windows </span><span class="hljs-string">Audio</span> |
While helpful, you might think there must be more information on the services running on a system than what is displayed here. Well, there is. By default, PowerShell uses default views to display common properties when commands are run. To see all of the properties available from a command, we use the pipeline, and a command called Get-Member. One of the commands everyone should know, Get-Member retrieves the ‘membership’ of an object by showing its properties and methods. This is accomplished by using a command’s output as input for get-member, like the syntax below.
1 2 | Some-Command | Get-Member <span class="hljs-keyword">or</span> gm (<span class="hljs-keyword">an</span> <span class="hljs-built_in">alias</span> <span class="hljs-keyword">for</span> <span class="hljs-built_in">get</span>-member) |
Now let’s see what information lies within the output of get-service.
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 41 | PS <span class="hljs-keyword">C</span>:GitHubdemos> get-service | <span class="hljs-keyword">Get</span>-Member TypeName: System.ServiceProcess.ServiceController Name MemberType <span class="hljs-keyword">Definition</span> ---- ---------- ---------- Name AliasProperty Name = ServiceName RequiredServices AliasProperty RequiredServices = ServicesDependedOn Disposed Event System.<span class="hljs-keyword">EventHandler</span> Disposed(System.Object, System.EventArgs) <span class="hljs-keyword">Close</span> <span class="hljs-keyword">Method</span> void <span class="hljs-keyword">Close</span>() <span class="hljs-keyword">Continue</span> <span class="hljs-keyword">Method</span> void <span class="hljs-keyword">Continue</span>() CreateObjRef <span class="hljs-keyword">Method</span> System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType) Dispose <span class="hljs-keyword">Method</span> void Dispose(), void IDisposable.Dispose() Equals <span class="hljs-keyword">Method</span> bool Equals(System.Object obj) ExecuteCommand <span class="hljs-keyword">Method</span> void ExecuteCommand(int command) GetHashCode <span class="hljs-keyword">Method</span> int GetHashCode() GetLifetimeService <span class="hljs-keyword">Method</span> System.Object GetLifetimeService() GetType <span class="hljs-keyword">Method</span> type GetType() InitializeLifetimeService <span class="hljs-keyword">Method</span> System.Object InitializeLifetimeService() <span class="hljs-keyword">Pause</span> <span class="hljs-keyword">Method</span> void <span class="hljs-keyword">Pause</span>() <span class="hljs-keyword">Refresh</span> <span class="hljs-keyword">Method</span> void <span class="hljs-keyword">Refresh</span>() Start <span class="hljs-keyword">Method</span> void Start(), void Start(string[] args) Stop <span class="hljs-keyword">Method</span> void Stop() WaitForStatus <span class="hljs-keyword">Method</span> void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desiredStatus), void WaitForStatus(System.ServiceProcess.ServiceCon... CanPauseAndContinue <span class="hljs-keyword">Property</span> bool CanPauseAndContinue {get;} CanShutdown <span class="hljs-keyword">Property</span> bool CanShutdown {get;} CanStop <span class="hljs-keyword">Property</span> bool CanStop {get;} Container <span class="hljs-keyword">Property</span> System.ComponentModel.IContainer Container {get;} DependentServices <span class="hljs-keyword">Property</span> System.ServiceProcess.ServiceController[] DependentServices {get;} DisplayName <span class="hljs-keyword">Property</span> string DisplayName {get;set;} <span class="hljs-keyword">MachineName</span> <span class="hljs-keyword">Property</span> string <span class="hljs-keyword">MachineName</span> {get;set;} ServiceHandle <span class="hljs-keyword">Property</span> System.Runtime.InteropServices.SafeHandle ServiceHandle {get;} ServiceName <span class="hljs-keyword">Property</span> string ServiceName {get;set;} ServicesDependedOn <span class="hljs-keyword">Property</span> System.ServiceProcess.ServiceController[] ServicesDependedOn {get;} ServiceType <span class="hljs-keyword">Property</span> System.ServiceProcess.ServiceType ServiceType {get;} Site <span class="hljs-keyword">Property</span> System.ComponentModel.ISite Site {get;set;} StartType <span class="hljs-keyword">Property</span> System.ServiceProcess.ServiceStartMode StartType {get;} Status <span class="hljs-keyword">Property</span> System.ServiceProcess.ServiceControllerStatus Status {get;} <span class="hljs-keyword">ToString</span> ScriptMethod System.Object <span class="hljs-keyword">ToString</span>(); |
From this we see there are a lot of properties not being displayed, actually 14 if we do a quick check.
1 2 3 | PS C:GitHubdemos> ( Get-Service | Get-Member | Where-Object -<span class="hljs-keyword">Property</span> Membertype -<span class="hljs-keyword">EQ</span> <span class="hljs-keyword">Property</span> ).count <span class="hljs-number">14</span> |
So now that you know the properties available for each get-service object, you use the data contained in each property anyway you see fit. For ease of demonstration, lets say you have a server whose services are not starting up properly, and you believe the issue may be due to dependent services not starting in a timely fashion. To assist, you want a list of all services including service name, service status, and the names of any services depended on (ServicesDependedOn) to start properly. Using format-table, we can view that in a flash.
1 2 3 4 5 6 7 8 9 10 11 12 | PS C:GitHubdemos> get-service | format-table -Property Name,Status,ServicesDependedOn Name Status ServicesDependedOn ---- ------ ------------------ AdobeARMservice <span class="hljs-keyword">Running</span> {} <span class="hljs-keyword">AJRouter</span> <span class="hljs-keyword">Stopped</span> {} <span class="hljs-keyword">ALG</span> <span class="hljs-keyword">Stopped</span> {} <span class="hljs-keyword">AppIDSvc</span> <span class="hljs-keyword">Stopped</span> {RpcSs, CryptSvc, AppID} <span class="hljs-keyword">Appinfo</span> <span class="hljs-keyword">Running</span> {RpcSs, ProfSvc} <span class="hljs-keyword">AppMgmt</span> <span class="hljs-keyword">Stopped</span> {} <span class="hljs-keyword">AppReadiness</span> <span class="hljs-keyword">Stopped</span> {} |
And there you have your table showing all the information you need.
Another great use for get-member is when working with Active Directory objects. As you can imagine, user objects have a bunch of properties, and they are not all displayed when using the Get-ADUser command. Using Get-Member will help you find all the properties for user objects.
Important Note With Get-ADUser and other Get- commands for Active Directory, the default usage only displays a subset of the properties available on a user object. To retrieve all the properties associated with an AD object, you need to add the parameter -Properties with the ‘*’ (or wildcard) value. Here’s how to use Get-Member to see all the user properties.
1 2 | <span class="hljs-keyword">Get</span>-ADUser -<span class="hljs-keyword">Properties</span> * | <span class="hljs-keyword">Get</span>-Member |
So next time you run into a PowerShell command and it is not giving you all the information you need, turn to Get-Member.
For more information on Get-Member, check out the docs here
For more information on Get-ADUser, check out the docs here