List Services With PowerShell

This article is also available on the Microsoft TechNet Wiki.

This article won a gold medal in The Microsoft TechNet Guru Awards! (December 2015).

Most people know you can see all the running services in a Windows operating system by employing services.msc through the Search box or Run command. From there, you can filter by Name or Startup Type to view what services are started, which are stopped, and which should have been started and perhaps failed.

You can also retrieve this information through PowerShell. The benefit of using PowertShell is that you can filter by Startup Type (startmode in PowerShell) or Status (state). For example, by using a PowerShell command you can find all services that have a Startup Type of Automatic and a Status of Stopped. This information is filtered and presented quickly to you. This saves you time from sorting and scrolling through the list of services.

Methodology

To find the services we are looking for, we will use the Get-WmiObject cmdlet within PowerShell. It is used to carry out system administration tasks and can even be run against remote computers. This cmdlet gets an instance of Windows Management Instrumentation (WMI) and any of its available sub-classes. We could also use its alias gwmi to shorten the cmdlet call.

Script To Find All Services

If you want to run a PowerShell command to find all services on your operating system, you can use a script similar to the one shown in the image below. It retrieves the Name, DisplayName, State, and StartMode of the services and then sorts them by State and Name. Setting the script up this way lets us see all of the Running and Stopped services grouped together. This provides a quick glimpse into services you suspect are causing a problem, especially if one is supposed to be Running and it is set to Stopped.

svc01

Script To Filter Services

If you need to be more precise in your searching, you can apply filters to your Get-WmiObject calls so that they return only the information you need. Using our example from above, to find all services that have a Startup Type of Automatic and a Status of Stopped we can apply a filter to return only services that meet this criteria.

svc02

Conclusion

In this article we saw how we can use the PowerShell Get-WmiObject cmdlet to find all the running services on an OS. We also saw how we can filter the data returned so it only shows the information we need to troubleshoot a problem. Using this filtering capability, we can see how much easier it is to quickly find services that meet our troubleshooting criteria.

References

Scripts

Download a copy of each PowerShell script from the TechNet Gallery:

They can also be found in my .

Related Articles

Azure PowerShell cmdlets version updates

This article is also available on the Microsoft TechNet Wiki.

This article won a bronze medal in The Microsoft TechNet Guru Awards – May 2015.

Microsoft Azure PowerShell provides cmdlets to developers and administrators so they can deploy and manage Microsoft Azure applications. These cmdlets are being updated constantly. To have the latest functionality you need to work with Azure you must update as well.

This wiki article will show you how to determine the current version of your Azure cmdlets and how you can update them to the latest version.

Prerequisites

Check Current Version

To determine your Azure PowerShell cmdlets version, follow these steps;

1. Open the Windows PowerShell Integrated Scripting Environment (ISE):

  • Click the Start menu and search for PowerShell ISE
  • In the Windows PowerShell console, the Command Prompt window, or in the Run box, type powershell_ise.exe.

2. Type the following script block into the top portion of your window:

(Get-Module -ListAvailable | Where-Object{ $_.Name -eq 'Azure' }) `
| Select Version, Name, Author, PowerShellVersion  | Format-List;

The script uses the Get-Module cmdlet to display the modules that have been imported, or that can be imported. The ListAvailable parameter displays all installed modules. We then select multiple properties from Get-Module to display. Finally, Format-List shows the output so that each property is listed on a new line.

3. Click the Run Script green arrow or press F5.

technet_powershell_01

The script will run and it will return something like this:

technet_powershell_02

As you can see I am running Version 0.8.3 of the Azure PowerShell cmdlets.

Update Cmdlets Version

All of the new Azure PowerShell cmdlets are housed on GitHub so it is easy to find the most current version.

Browse to Microsoft Azure PowerShell releases to locate the latest release. It will be at the top of the page marked with a green label. As of this writing the latest release is 0.9.1.

technet_powershell_03

To download and install the modules you can use the  Microsoft Web Platform Installer. Once the Web Platform Installer is installed and running it should open up on the Spotlight tab. Since this is an upgrade the new Microsoft Azure PowerShell package should be available for addition.

technet_powershell_04

Double clicking on the name of the package will provide more details. Notice that the package listed here is consistent with the latest release on GitHub.

technet_powershell_05

You can also use the Windows Standalone PowerShell installer to download and run the .msi file. When using the .msi package it is a matter of double clicking the downloaded file to begin the installation. The installer will ask you to agree to the terms of the license before you are allowed to install.

technet_powershell_06

Once the install is complete you will get a success message on the standalone installer dialog box.

technet_powershell_07

Verify New Version

Whatever way you choose to install the updated version it will need to be verified once it is completed.

If the Windows PowerShell ISE is still open close it down and restart it.

Re-run the same code in the ISE from the Check Current Version section. Your PowerShell results should now look similar to this:

technet_powershell_08

Conclusion

In this article we saw how to check the Azure PowerShell cmdlets version. We then saw two ways to install the cmdlets updates and how to verify that the upgrade worked correctly.

References

Scripts

To download a version of this script visit my . It can also be found in the TechNet Gallery under Check Azure PowerShell cmdlets version (PowerShell) at https://gallery.technet.microsoft.com/Azure-PowerShell-cmdlets-13dc4937.

PowerShell: Invoke-WebRequest and URL links

I was working on a project recently that needed to count the number of links on a given URL. With the code complete I wanted a simple way to validate my work to make sure my totals matched. So, I turned to Windows PowerShell for a quick test and to continue my learning of this powerful tool.

PowerShell 3.0 introduced the Invoke-WebRequest cmdlet that makes it easy to get content from a URI passed in as a parameter. The cmdlet goes out to the internet and returns collections of forms, links, images, and other significant HTML elements from a web page.

To run the command all you need is a webpage address as a parameter:

Invoke-WebRequest

From the image above you can see there is a lot of information returned. We can see the StatusCode, Content, Forms, etc. Each of these properties returned is also available for further inspection. To see a full list we can use the Get-Member cmdlet to retrieve them all. The cmdlet also allows for the “gm” alias to be used.

Invoke-WebRequest -Uri 'http://www.lego.com/en-us/' | gm -MemberType Property

We can see from the list below that all of the properties returned matches the content in the image above:

Invoke-WebRequest Properties

From the list I see the Links property is available which is exactly what I need for my code validation. However, just adding .”Links” to my Invoke-WebRequest command will only return the hyperlinks themselves. This might be all you need if you want to ensure a returned webpage contains a link you are looking for. However, since I need a sum of links I will append the Count method so I can see the final total.

Invoke-WebRequest URL Link Count

To see a parameterized version of this script visit my . It can also be found in the TechNet Gallery under Count webpage links within a given URL (PowerShell) at https://gallery.technet.microsoft.com/Count-webpage-links-within-6c00db86.

If you have solved any problems with a cool PowerShell hack please leave a comment below.

Related Articles:

Publishing scripts to TechNet

Publishing scripts to TechNet

I have been experimenting with PowerShell for a while now and I have come to the conclusion that it is a very powerful tool. I like being able to manipulate various objects in the operating system so that I can automate everyday tasks.PowerShell Logo One of the things I do a lot of is search my system for files. Normally I look by name but sometimes I need to search by file extension. So, I thought this would be the perfect candidate for a script. Now, this blog post will not go into great detail on what this script does. If you want to view it or download it you can find it in my . Similarly, I will assume you know how to run scripts in PowerShell. If you have never used PowerShell then now is the time to learn! There are lots of tutorials on the web. You can start with the Windows PowerShell User’s Guide. Instead, this post is about how you can contribute scripts to TechNet on the Microsoft site. If you are unfamiliar with TechNet consider it a portal containing information and technical resources for Microsoft products.

TechNet Galleries

There are two areas on TechNet that you can post to. One is the TechNet Gallery. This space includes scripts as well as resources for all of the current Microsoft operating systems. The other is the TechNet Script Center Repository. This is a section of TechNet dedicated solely to scripts written in VB Script, PowerShell, SQL, JavaScript, or other scripting languages. Upon reading up on the differences between the two it seemed that in previous years you needed to post a script to both sites individually. However, my experience was that posting to the Script Center Repository replicated it to the TechNet Gallery. If you want some background on why it is prudent to have your script in both galleries you can check out this article entitled How to Publish your Script to Both the TechNet Gallery and the TechNet Script Center Repository. Author Craig Lussier says that by “having your script listed in both the Gallery and Script Center Repository you can ensure that there are more access points for your contribution for the community at large. Before you can begin inserting your script you must have a Microsoft account. You can sign up for one on the TechNet site. This account can be used here and is also the same account you would use on the MSDN forums if you have questions. For those of you who have not been using TechNet or MSDN lately you will also see that Microsoft has added a badging system like StackOverflow’s to your profile. You can see an example on my profile below:

How To Upload

Once you are logged into TechNet you will see an area on the left-hand side entitled “Quick Access.” Under this title there will be an “Upload a contribution” link. Click it to begin the process. Use the “File upload” section to browse to your PowerShell (*.ps1) or other script file. Then give your contribution a title that describes your script. For mine I am calling it “Get files by extension within a given path (PowerShell).” I added the script type to the header so that it is clear within the search results what type of script it is. Next, use the “Description” area to provide more information about your contribution. You can add code snippets, screenshots, additional files and even HTML to illustrate your script. To see an excellent example of a description with HTML, images and code snippets check out this highly rated contribution. TechNet Upload area For my description I am adding a summary of what the user should expect when they run the script. I am also adding my full PowerShell file using the “Insert Code Section” button on the description toolbar. To do this I just needed to select my script language and then paste my code into the left-hand side. A preview is automatically generated on the right-hand side for me. If you are happy with the preview click the “Insert” button to add paste it in. TechNet Insert Code Section The “Summary” is what will be displayed in the search results. You can default to using the initial 280 characters of your description or you can write a new one. The site will raise an error if your description has less than 280 characters though. You will also need to set the language of your description. TechNet Upload area Choosing a Category was tricky for me as the script is more of a productivity item. I searched the Script Center Repository for similar scripts to see which category others chose. I eventually went for the Storage category. I then chose the Files sub category. Tip: To find a category you could always browse through the sub categories first to see if a narrower topic can help define your top-level category. TechNet Upload area Now set the “Operating systems” that you have tested the script on. You can then assign tags to the submission so that it is easier to find and categorize. The list is auto-populated so you can type in as many as you need. Again, looking at similar scripts in the repository can help you choose tags that are appropriate. Under “Options” you can then elect to have the Questions and Answers tab on your contribution. One of the benefits of adding your script to TechNet is that people can comment on it and ask questions. I have also seen commenters provide alternate ways of accomplishing the same tasks which allows you to learn even more. Next, choose the “License” that appeals to you and then accept the Terms of use. Take some time to read the terms. If this is proprietary code then this section might be of particular interest:

We do not claim ownership of any Submission unless otherwise agreed to by the parties. However, by providing a Submission, you are irrevocably granting Microsoft and its Affiliates the right to make, use, modify, distribute and otherwise commercialize the Submission in any way and for any purpose (including by granting the general public the right to use your Submissions in accordance with this Agreement, which may change over time).

Finally, if you are happy with the results click “Save and publish.” TechNet Upload area

How The Script Appears

Clicking “Save and publish” makes the script appear instantly in the TechNet Script Center Repository. Once published the contribution URL can be found under “My contributions” in the “Quick Access” area. A quick search of both the script name and of my last name in the TechNet Script Center Repository confirms that the script is now live. A similar search in the TechNet Gallery also displays my script. So, officially my Get files by extension within a given path (PowerShell) script can now be found at . One interesting thing to note is that I now have an additional link under the “Quick Access” area called “Browse script requests.” This link lists scripts that were requested by other community members. This will definitely be a great starting point for future PowerShell project ideas.

Conclusion

As you can now see I have a PowerShell script in both GitHub and the TechNet galleries. The whole process was very intuitive and quick. I encourage you to think about contributing your scripts to TechNet. Please download my script and try to run it on your system. As well, please leave a question or a rating on a TechNet gallery page. Hopefully you have found this blog post useful. If you have any questions or comments please leave me a message below.