{"id":196,"date":"2021-02-23T16:44:14","date_gmt":"2021-02-24T00:44:14","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/powershell-community\/?p=196"},"modified":"2021-02-27T09:33:11","modified_gmt":"2021-02-27T17:33:11","slug":"using-get-service-in-powershell-7-vs-windows-powershell-5-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell-community\/using-get-service-in-powershell-7-vs-windows-powershell-5-1\/","title":{"rendered":"Using Get-Service in PowerShell 7 vs. Windows PowerShell 5.1"},"content":{"rendered":"<p><strong>Q<\/strong>: How can I get the Username, and StartType for a Windows Service?<\/p>\n<p><strong>A<\/strong>: Quick answer is PowerShell 7.<\/p>\n<p>Microsoft is doing a great job on PowerShell with each version they release. The simple answer to this question is a command called <code>Get-Service<\/code>. But there is a big update that makes getting the required information much easier with PowerShell 7. I will show the result of this command using both <strong>PowerShell 7<\/strong> and <strong>Windows PowerShell 5.1<\/strong>.<\/p>\n<p>Let&#8217;s start by typing the simple command <code>Get-Service Workstation<\/code>. This command return basic details for a service called <strong>Workstation<\/strong>. The result is the same for both PowerShell 7 and Windows PowerShell 5.1.<\/p>\n<pre><code class=\"powershell\">Status   Name               DisplayName\n------   ----               -----------\nRunning  LanmanWorkstation  Workstation\n<\/code><\/pre>\n<p>To drill-down and get a more detailed result, we need to see all the associated <em>properties<\/em> and <em>methods<\/em> for this service, which can be achieved using the following command.<\/p>\n<h2>Getting Windows Services using Get-Service<\/h2>\n<pre><code class=\"powershell\">Get-Service Workstation | Get-Member | Select-Object Name, MemberType\n<\/code><\/pre>\n<p>The output returns a list of members that can be invoked in the command line. Here is the output in PowerShell 7.<\/p>\n<pre><code class=\"powershell\">Name                         MemberType\n----                         ----------\nName                      AliasProperty\nRequiredServices          AliasProperty\nDisposed                          Event\nClose                            Method\nContinue                         Method\nDispose                          Method\nEquals                           Method\nExecuteCommand                   Method\nGetHashCode                      Method\nGetLifetimeService               Method\nGetType                          Method\nInitializeLifetimeService        Method\nPause                            Method\nRefresh                          Method\nStart                            Method\nStop                             Method\nWaitForStatus                    Method\nBinaryPathName                 Property\nCanPauseAndContinue            Property\nCanShutdown                    Property\nCanStop                        Property\nContainer                      Property\nDelayedAutoStart               Property\nDependentServices              Property\nDescription                    Property\nDisplayName                    Property\nMachineName                    Property\nServiceHandle                  Property\nServiceName                    Property\nServicesDependedOn             Property\nServiceType                    Property\nSite                           Property\nStartType                      Property\nStartupType                    Property\nStatus                         Property\nUserName                       Property\nToString                   ScriptMethod\n<\/code><\/pre>\n<p>Here is the output in Windows PowerShell 5.1.<\/p>\n<pre><code class=\"powershell\">Name                         MemberType\n----                         ----------\nName                      AliasProperty\nRequiredServices          AliasProperty\nDisposed                          Event\nClose                            Method\nContinue                         Method\nCreateObjRef                     Method\nDispose                          Method\nEquals                           Method\nExecuteCommand                   Method\nGetHashCode                      Method\nGetLifetimeService               Method\nGetType                          Method\nInitializeLifetimeService        Method\nPause                            Method\nRefresh                          Method\nStart                            Method\nStop                             Method\nWaitForStatus                    Method\nCanPauseAndContinue            Property\nCanShutdown                    Property\nCanStop                        Property\nContainer                      Property\nDependentServices              Property\nDisplayName                    Property\nMachineName                    Property\nServiceHandle                  Property\nServiceName                    Property\nServicesDependedOn             Property\nServiceType                    Property\nSite                           Property\nStartType                      Property\nStatus                         Property\nToString                   ScriptMethod\n<\/code><\/pre>\n<h2>PowerShell 7<\/h2>\n<p>The big difference is in the <strong>Property<\/strong> members. Now, in PowerShell 7, it&#8217;s possible to read some additional properties that were not available in Windows PowerShell 5.1, such as <strong>UserName<\/strong>, <strong>BinaryPathName<\/strong>, <strong>StartType<\/strong>. So let&#8217;s see how to read these properties using PowerShell 7.<\/p>\n<pre><code class=\"powershell\">PS 7&gt; Get-Service workstation | select  Username,Starttype,BinaryPathName\n<\/code><\/pre>\n<p>The output is a clear, with all the required results using a native one-liner command.<\/p>\n<pre><code class=\"powershell\">UserName                    StartType BinaryPathName\n--------                    --------- --------------\nNT AUTHORITY\\NetworkService Automatic C:\\WINDOWS\\System32\\svchost.exe -k NetworkService -p\n<\/code><\/pre>\n<h2>Windows Powershell 5.1<\/h2>\n<p>For Windows PowerShell 5.1, the operation is not as simple as in PowerShell. We need to use the <code>Get-CimInstance<\/code> and pass the required WQL query. So, in Windows PowerShell 5.1, run the following command:<\/p>\n<pre><code class=\"powershell\">WPS 5.1&gt; Get-CimInstance -Query 'select * from Win32_Service where caption like \"Workstation\"' | select StartName,StartMode,PathName\n<\/code><\/pre>\n<pre><code class=\"powershell\">StartName                   StartMode PathName\n---------                   --------- --------\nNT AUTHORITY\\NetworkService Auto      C:WINDOWS\\System32\\svchost.exe -k NetworkService -p\n<\/code><\/pre>\n<p>In this example, we invoke <code>Get-CimInstance<\/code> with a query to get the service name and then select the required properties, which is a long way and requires you to know extra information related to the original service name and some basic WMI query language.<\/p>\n<h2>Summary<\/h2>\n<p>More and more to come with PowerShell 7, ease of use, backward compatibility much rich experience. This post shows a small portion of a small change in PowerShell which will help a lot of admin in their day-to-day tasks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: How can I get the Username, and StartType for a Windows Service? A: Quick answer is PowerShell 7. Microsoft is doing a great job on PowerShell with each version they release. The simple answer to this question is a command called Get-Service. But there is a big update that makes getting the required information [&hellip;]<\/p>\n","protected":false},"author":53227,"featured_media":77,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-196","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Q: How can I get the Username, and StartType for a Windows Service? A: Quick answer is PowerShell 7. Microsoft is doing a great job on PowerShell with each version they release. The simple answer to this question is a command called Get-Service. But there is a big update that makes getting the required information [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/users\/53227"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}