PowerShell is a powerful scripting language that makes automating Windows system admin tasks a breeze. If you’re new to PowerShell scripting, you may feel overwhelmed. There are so many different commands it can take a lot of work to know where to start. To help make your journey into PowerShell scripting easier, here are some of the top commands every new PowerShell scripter needs to know.
PowerShell Commands Name Structure
PowerShell commands are structured using a “verb–noun” naming pattern. This pattern creates a uniform way of defining commands, making them easier to understand and use. Verbs are used to describe the action that the command will perform, and nouns are used to refer to the items the command will act upon.
For example, the “Get-Process” command will retrieve a list of currently running processes on a computer. The verb “Get” indicates that the command will retrieve, and the noun “Process” indicates that the command will do so for processes. Knowing this structure allows intuitively knowing the command you need to use for a task without knowing the exact command.
Get-Help is a command that will provide you with help and examples of using other commands in PowerShell. To use it, type Get-Help, followed by the command you need help with. The command is straightforward but one you will use frequently.
PS> Get-Help Get-Content
The Get-Command command searches for other PowerShell commands. To use it, type Get-Command followed by the command name. When you combine your knowledge of the command naming structure and Get-Command, you can quickly find other commands for a task. Then use Get-Help to confirm how the command works.
PS> Get-Command *process*
Get-Member allows you to find information about the objects that PowerShell outputs, including the object type, methods, properties, and members. Since every PowerShell command outputs an object with multiple properties, you will use this frequently.
Additionally, this command can be used to create custom objects and display the object’s structure. Showing an object’s structure is especially helpful for troubleshooting, as users can view and analyze the objects used in their scripts.
Get-Process | Get-Member
In the above image, you can see the object’s properties created by the Get-Process command. There is a “Name” and “ID”(PID) property. So I could get a single processes properties and use just the process Name or PID value in my script. To see how to grab select values of an object, read on.
The Select-Object PowerShell command selects specific properties from an object and displays them in the output. In this example, the command “Get-Process -ProcessName notepad++” is used to retrieve the process object of Notepad++. This object is then piped into the Select-Object command, which can isolate the “Name” and “ID” values from the object, thereby limiting the output to just these values.
Get-Process -ProcessName notepad++ | Select-Object Name, ID
Let’s use this in a simple script.
# Example use in a script. # Create two variable, one with the notepad++ PID and one with name, then output to the console. $ProcessData = Get-Process -ProcessName notepad++ $NotepadID = $ProcessData | Select-Object -ExpandProperty ID $NotepadName = $ProcessData | Select-Object -ExpandProperty Name Write-Host "Notepad++ has the process name $NotepadName and a Process ID(PID) of $NotepadID"
Side note: The script above is not the most efficient way to script this. It is only a way of explaining the concept. A better version of this same script is as follows.
$ProcessData = Get-Process -ProcessName notepad++ Write-Host 'Notepad++ has the process name' $ProcessData.Name 'and a Process ID(PID) of' $ProcessData.ID
So you can see, there are many ways to work with object properies in a script.
The Format-List and Format-Table PowerShell commands are used to format the output of a command, allowing users to customize how the output appears. To use them, the command should be followed by the Format command and the parameter names that should be displayed.
For example, to output details created by the command “Get-Process -ProcessName notepad++“, the command could be written as follows.
Get-Process -ProcessName notepad++ | Format-Table -Property Name, ID, CPU, WorkingSet, FileName
This would format the output to show the Name, CPU, WorkingSet, and FileName of the process.
Let’s take the same data and use it in the Format-List comamnd this time.
Get-Process -ProcessName notepad++ | Format-List -Property Name, ID, CPU, WorkingSet, FileName
The Out-File command allows you to take the output of a PowerShell command and save it to a file of your choosing. The saved files could create a log for troubleshooting or save results from a command for later use. Out-File also allows you to append data to an existing file or overwrite it. The command is also helpful for creating files with specific formats, such as CSV or XML, as it allows you to specify the file type when running the command.
Get-Process -ProcessName notepad++ | Out-File out.txt
This command is simple and streightforward, but have many uses when you are scripting.
The Export-Csv PowerShell command can export large data sets, such as the command “Get-Process” output, into a comma-separated values (CSV) file. For further analysis, the exported file will work with any spreadsheet program, such as Microsoft Excel. The data is stored in the same order as it is output from the command, which makes it easy to identify columns and rows of data. Another use of the Export-Csv command is exporting data from other data sources, such as databases or web APIs.
Let’s see how we can create a CSV file of all the processes and their data in one file.
Get-Process | Export-Csv -Path ./Processes.csv
Now we can open the “Processes.csv” file in MS Excel.
Knowing these basics will help you get started scripting out tasks and reports. If you want to improve your scripting skills, start thinking of day-to-day tasks that you have to do and then write a script to do it for you. Automate the process with a script if you have to do any task multiple times a week or day. Scripting is the defining skill that separates mediocre System Admins from expert-level System Admins.