{"id":17251,"date":"2010-09-01T00:01:00","date_gmt":"2010-09-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/01\/use-windows-powershell-to-display-service-dependencies\/"},"modified":"2010-09-01T00:01:00","modified_gmt":"2010-09-01T00:01:00","slug":"use-windows-powershell-to-display-service-dependencies","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-windows-powershell-to-display-service-dependencies\/","title":{"rendered":"Use Windows PowerShell to Display Service Dependencies"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use Windows PowerShell to display service dependencies on Windows for troubleshooting shutdown problems in Windows 7.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/><\/p>\n<p> Hey, Scripting Guy! I have a problem on my Windows 7 PC. It hangs on shutdown from time to time. I have done some searches on the Internet, and it seems other people have reported similar problems. The cause appears to be related to either crappy video drivers or crappy audio drivers, depending on whose website I am reading. One site suggested creating a .bat file to stop the services manually before shutting down the workstation. I would like to use Windows PowerShell to write such a script, but I need to find out information about the service dependencies. I looked at using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394582(VS.85).aspx\">WMI<\/a> to find service dependencies, but I do not see any information via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394418(VS.85).aspx\">Win32_Service<\/a> WMI class. Can you help me?<\/p>\n<p>&#8212; VG<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/><\/p>\n<p> Hello VG, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. One of the cool things about <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> is that it allows you to communicate with thousands of people instantaneously. One of the cool things about Windows PowerShell is that the code can be so short, it fits into the limitations of a tweet. This makes Twitter and Windows PowerShell an awesome combination. As a case in point, I posted a tweet about listing colors that are used by the <strong>Write-Host<\/strong> cmdlet. One of the Scripting Guy <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en-US\/ITCG\/threads\">forum<\/a> moderators saw the tweet and posted a line of code he uses to display both the console colors and the name of the console color. The cool thing is that I was familiar with the .NET Framework class he used, but I had never written that line of code before. I added it to the discussion in this post. Way cool! While I had Trevor on the line, I also asked him if he would like to write a guest blog article. He said yes, so we have that to look forward. <\/p>\n<p>VG, none of that has anything to do with services, but it has to do with the display of service names in the Get-ServiceDependencies.ps1 script that is shown here. <\/p>\n<blockquote>\n<p><strong>Get-ServiceDependencies.ps1<\/strong><\/p>\n<p>Get-Service -CN . | Where-Object { $_.status -eq &#8216;running&#8217;} |     <br \/>ForEach-Object {      <br \/> write-host -ForegroundColor 9 &#8220;Service name $($_.name)&#8221;      <br \/>&nbsp; if($_.DependentServices)      <br \/>&nbsp;&nbsp;&nbsp; { write-host -ForegroundColor 3 &#8220;`tServices that depend on $($_.name)&#8221;      <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach($s in $_.DependentServices)      <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &#8220;`t`t&#8221; + $s.name }      <br \/>&nbsp;&nbsp;&nbsp; } #end if DependentServices      <br \/>&nbsp; if($_.RequiredServices)      <br \/>&nbsp;&nbsp;&nbsp; { Write-host -ForegroundColor 10 &#8220;`tServices required by $($_.name)&#8221;      <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach($r in $_.RequiredServices)      <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &#8220;`t`t&#8221; + $r.name }      <br \/>&nbsp;&nbsp;&nbsp; } #end if DependentServices      <br \/>} #end foreach-object<\/p>\n<\/blockquote>\n<p>The Get-ServiceDependencies.ps1 script uses the <strong>Get-Service<\/strong> cmdlet to retrieve information about services on the computer. You can use the <strong>Get-Service<\/strong> cmdlet to run against a local computer or a remote computer. When running against a remote computer, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa366453(v=VS.85).aspx\">Windows Firewall<\/a> must be modified to permit the cmdlet through the firewall. In addition, you must have administrative permission on the remote machine. Unfortunately, there is neither a <strong>&ndash;filter<\/strong> parameter for <strong>Get-Service<\/strong>, nor a <strong>&ndash;credential<\/strong> parameter. This means that all services are returned by the <strong>Get-Service<\/strong> cmdlet. It also means that you cannot supply alternative credentials for the <strong>Get-Service<\/strong> cmdlet. <\/p>\n<p>The period is a shortcut to specify the local host; it works for the <strong>Get-Service<\/strong> cmdlet and other cmdlets. Because there is no <strong>filter<\/strong> parameter for the <strong>Get-Service<\/strong> cmdlet, it is necessary to <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\">pipe<\/a> the results of the <strong>Get-Service<\/strong> cmdlet to the <strong>Where-Object<\/strong> cmdlet. The property status is checked and if the status indicates the service is running, the <strong>ServiceController<\/strong> object is passed along the pipeline to the <a href=\"http:\/\/www.bing.com\/visualsearch?g=powershell_cmdlets&amp;FORM=Z9GE22\">ForEach-Object cmdlet<\/a>. Each service is an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.serviceprocess.servicecontroller.aspx\">System.ServiceProcess.ServiceController<\/a> .NET Framework class and as such has a large number of properties and methods available to it. Check MSDN for all the things you can do with the <strong>ServiceController<\/strong> class. The part of the script that retrieves all the services on the local computer and filters them out to find only the ones that are running is shown here:<\/p>\n<blockquote>\n<p>Get-Service -CN . | Where-Object { $_.status -eq &#8216;running&#8217;} |<\/p>\n<\/blockquote>\n<p>The <strong>ServiceContoller<\/strong> .NET Framework class has two properties that are extremely interesting. The first one is the <strong>dependentServices<\/strong> property, and the second one is the <strong>RequiredServices<\/strong> property. This makes it easy to find service dependencies. The <strong>ForEach-Object<\/strong> cmdlet takes each <strong>ServiceController<\/strong> object as it comes across the pipeline, and allows us to work with them individually. The <strong>Write-Host<\/strong> cmdlet is used to write the name of the service to the Windows PowerShell output in color. <\/p>\n<p>The color combinations that can be used with the Write-Host cmdlet are limited to 16 colors. However, when selecting colors, I find it hard to visualize how yellow, or green might show up. Therefore, I use a simple line of code to display all the colors to the Windows PowerShell ISE output pane. This is seen here. <\/p>\n<blockquote>\n<p>0..15 | % { Write-Host -ForegroundColor $_ &#8220;$_&#8221; }<\/p>\n<p>The 0..15 line creates an array of 16 numbers. This is seen here.<\/p>\n<p>PS C:\\&gt; $a = 0..15     <br \/>PS C:\\&gt; $a.GetType()<\/p>\n<p>IsPublic IsSerial&nbsp;&nbsp;&nbsp; Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BaseType     <br \/>&#8212;&#8212;&#8211;&nbsp;&nbsp; &#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;      <br \/>True&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object[]&nbsp;&nbsp;&nbsp;&nbsp; System.Array<\/p>\n<p>PS C:\\&gt;<\/p>\n<\/blockquote>\n<p>Each of the numbers is passed over the pipeline to the <strong>ForEach-Object<\/strong> cmdlet. The <strong>%<\/strong> symbol is an alias for the <strong>ForEach-Object<\/strong> cmdlt. The <strong>$_<\/strong> character refers to the current number on the pipeline. It therefore changes the foreground color to the color represented by the number. As seen in the following image, some of the colors are nearly invisible, and many of the other colors are pretty well washed out. It then becomes a simple matter to select colors that you like and that show up well in the Windows PowerShell ISE output pane. Keep in mind that if you have <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/08\/29\/customize-colors-and-fonts-in-the-windows-powershell-ise.aspx\">changed the color scheme for the Windows PowerShell ISE<\/a> by following the discussion in our recent <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/windows+powershell+ise\/\">Weekend Scripter posts about the ISE<\/a>, you will need to choose different color values for your environment. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5557.HSG09011001_3DE8ECE9.jpg\"><img decoding=\"async\" height=\"444\" width=\"554\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7711.HSG09011001_thumb_75BB310C.jpg\" alt=\"Image of colors in Windows PowerShell ISE\" border=\"0\" title=\"Image of colors in Windows PowerShell ISE\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>Trevor Sullivan (pcgeek86 on Twitter), one of the moderators on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a> and a consultant from Chicago, saw my tweet about the above line of code to display colors via <strong>Write-Host<\/strong>, and he mentioned that he likes to see the color names. To display the colors and the color names, he queries the static properties from the <strong>system.consolecolor<\/strong> .NET Framework class. He uses the <strong>Get-Member<\/strong> cmdlet to retrieve the static properties. Static properties of the <strong>consolecolor<\/strong> class are shown here:<\/p>\n<blockquote>\n<p>PS C:\\&gt; [consolecolor] | gm -st -m property<\/p>\n<p>&nbsp;&nbsp; TypeName: System.ConsoleColor<\/p>\n<p>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor Blue {get;}      <br \/>Cyan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor Cyan {get;}      <br \/>DarkBlue&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor DarkBlue {get;}      <br \/>DarkCyan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor DarkCyan {get;}      <br \/>DarkGray&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor DarkGray {get;}      <br \/>DarkGreen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor DarkGreen {get;}      <br \/>DarkMagenta&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor DarkMagenta {get;}      <br \/>DarkRed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor DarkRed {get;}      <br \/>DarkYellow&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor DarkYellow {get;}      <br \/>Gray&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor Gray {get;}      <br \/>Green&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor Green {get;}      <br \/>Magenta&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static System.ConsoleColor Magenta {get;}      <br \/>Red&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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bsp;&nbsp;&nbsp; static System.ConsoleColor Yellow {get;}<\/p>\n<\/blockquote>\n<p>He then pipelines the property names to the <strong>Write-Host<\/strong> cmdlet to display the color name in color. (Who&rsquo;s on first?) Here is his line of code:<\/p>\n<blockquote>\n<p>[consolecolor] | gm -st -m property | % { write-host -f $_.Name $_.Name }<\/p>\n<\/blockquote>\n<p>Back to the Get-ServiceDependencies.ps1 script. When the service name has been displayed, if there are any dependent services, this fact is displayed by tabbing over one spot, displaying the service name, and stating there are dependencies. The <strong>ForEach<\/strong> statement is used to walk through the collection of dependent services, and the dependent service name is displayed two tab spots over. This portion of the script is shown here:<\/p>\n<blockquote>\n<p>ForEach-Object {     <br \/> write-host -ForegroundColor 9 &#8220;Service name $($_.name)&#8221;      <br \/>&nbsp; if($_.DependentServices)      <br \/>&nbsp;&nbsp;&nbsp; { write-host -ForegroundColor 3 &#8220;`tServices that depend on $($_.name)&#8221;      <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach($s in $_.DependentServices)      <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &#8220;`t`t&#8221; + $s.name }      <br \/>&nbsp;&nbsp;&nbsp; } #end if DependentServices<\/p>\n<\/blockquote>\n<p>If the service requires other services, those services can be detailed from the <strong>RequiredServices<\/strong> property. The <strong>if<\/strong> statement is used to determine if there are any required services. If there are, the <strong>Write-Host<\/strong> cmdlet is used to display the service name and each of the required services. The required services are stored as an array in the <strong>RequiredServices<\/strong> property, and after tabbing over two spots, the name of the required service is printed out. This portion of the script is shown here:<\/p>\n<blockquote>\n<p>if($_.RequiredServices)<\/p>\n<p>&nbsp;&nbsp;&nbsp; { Write-host -ForegroundColor 10 &#8220;`tServices required by $($_.name)&#8221;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach($r in $_.RequiredServices)<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &#8220;`t`t&#8221; + $r.name }<\/p>\n<p>&nbsp;&nbsp;&nbsp; } #end if DependentServices<\/p>\n<\/blockquote>\n<p>When the Get-ServiceDependencies.ps1 script runs, the output is displayed that is shown in the following image. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8780.HSG09011002_6E2FC19F.jpg\"><img decoding=\"async\" height=\"433\" width=\"554\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2043.HSG09011002_thumb_1B44AE6E.jpg\" alt=\"Image of output displayed when script is run\" border=\"0\" title=\"Image of output displayed when script is run\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>VG, that is all there is to using Windows PowerShell to retrieve information about service dependencies. WMI Week will continue tomorrow when we will continue to talk about working with services. <\/p>\n<p>We would love for you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Windows PowerShell to display service dependencies on Windows for troubleshooting shutdown problems in Windows 7. &nbsp; Hey, Scripting Guy! I have a problem on my Windows 7 PC. It hangs on shutdown from time to time. I have done some searches on the Internet, and it seems other people have reported similar problems. [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,3,39,45],"class_list":["post-17251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scripting-guy","tag-services","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use Windows PowerShell to display service dependencies on Windows for troubleshooting shutdown problems in Windows 7. &nbsp; Hey, Scripting Guy! I have a problem on my Windows 7 PC. It hangs on shutdown from time to time. I have done some searches on the Internet, and it seems other people have reported similar problems. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17251"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17251\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}