Contact

Technology

Sep 22, 2014

5 PowerShell Basics You Need to Know

Bobby Crotty

Bobby Crotty

Default image background

PowerShell can automate most repetitive tasks in Microsoft environments, and all you need to know are a few basics to get started. PowerShell has two main functions: getting information and setting information. In this post we’ll cover five basics of getting and using information with PowerShell.

1.   FINDING INFORMATION

When I’m tackling a new problem with PowerShell, my first stop is usually at Microsoft’s TechNet Library to find more information about a particular cmdlet (compiled PowerShell commands). If I don’t know which cmdlet I need to be using, searching for what I want to do usually points me in the right direction. The Hey, Scripting Guy! Blog is one of my favorite resources, and it usually covers whatever I’m trying to do.

Within PowerShell, there are three commands that will help you. Get-Help shows you the PowerShell documentation on cmdlets, which is the same information TechNet uses. There are several useful parameters you can pass to Get-Help such as -Examples, which returns only examples of the particular cmdlet being used, and -Parameter <string>, which only shows information about the specified parameter.

Get-Help Get-ChildItem -ExamplesGet-Help Get-ChildItem -Parameter Filter

The Get-Command cmdlet assists you in looking up names of cmdlets. For example, to find all cmdlets with “service” in the name, use the following command:

Get-Command *Service*

Sometimes when you’re troubleshooting a command you need to know more about an object and what it can do. The Get-Member command (or GM for short) tells you the type of an object, and it also lists the members of an object, typically methods and properties. Methods are functions inside the object that do something with or to the object. Properties are pieces of information that the object stores, such as length or count. If you need to know what properties an Active Directory object contains, for example, you can use the following command:

Get-ADUser Administrator -Properties * | GM

2.   FILTERING INFORMATION

As an IT guy, I’m constantly getting information about users and computers in the environments I work in. The problem is that PowerShell often returns too much information. There are several commands that can help you target the information you need. Let’s say you want a list of all the computers in an environment that are running Windows 8. You could export all the computer objects and then delete the ones that don’t match, but the better way is to use the Where-Object cmdlet (or Where or ? for short), which allows you to filter out results based on defined criteria. When using Where-Object, the command is:

Get-ADComputer -Filter * -Properties OperatingSystem | ?{$_.OperatingSystem -like "Windows 8*"}

Note: The $_ variable is like this in C++ and refers to the object that is currently being passed.

3.   REFINING INFORMATION

When you run that command, the output is still not easy to read because it returns many more properties than we need. The Select-Object cmdlet (or Select for short) allows you to only show the properties that you choose. The following command gives you a list of Windows 8 computer names and the specific operating system.

Get-ADComputer -Filter * -Properties OperatingSystem | ?{$_.OperatingSystem -like "Windows 8*"} | Select Name,OperatingSystem

Select returns from an object only the fields you specify, but you can also use it to incorporate information that is not contained in the object using a custom field. For example, the user account security identifier (SID) and which database the mailbox is in are contained in the Active Directory user account and the Exchange mailbox object. To create a table with both, use the following command from the Exchange Management Shell:

Get-Mailbox –Filter * | Select Name,Database,@{Name="SID"; Expression={(Get-ADUser $_.SamAccountName -Properties SID).SID}}

4.   ANALYZING INFORMATION

From our operating system example above, you can see which operating system each computer is running, but what if you just want to know the totals for each? The Group-Object cmdlet (or Group for short) is what you’re looking for. It allows you to summarize information that other commands return. The following command, for example, will return the number of computer objects using each different operating system:

Get-ADComputer -Filter * -Properties OperatingSystem | Group OperatingSystem

5.   EXPORTING INFORMATION

Sometimes you don’t just want to see the information on screen, but you want to export it as well. The Export-Csv cmdlet works very well with the Select cmdlet. It takes any information that comes from Select and exports a CSV file that can be opened in Excel. The -NoTypeInformation parameter removes some header lines that are usually not needed.

Get-ADUser -Filter * | Select Name,SamAccountName | Export-Csv “c:\Users.csv” –NoTypeInformation

What PowerShell tricks do you have up your sleeve? Feel free to share your tips and tricks in the comments below or send us a tweet @CrederaIT.

Conversation Icon

Contact Us

Ready to achieve your vision? We're here to help.

We'd love to start a conversation. Fill out the form and we'll connect you with the right person.

Searching for a new career?

View job openings