Top PowerShell Commands for Beginners

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.

More on the “verb-noun” naming method can be found here.


Get-Help

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
Top PowerShell Commands for Beginners Get-Help

Get-Command

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*
Top PowerShell Commands for Beginners Get-Command

Get-Member

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
Top PowerShell Commands for Beginners 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.


Select-Object

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
Top PowerShell Commands for Beginners Select-Object

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"
Top PowerShell Commands for Beginners Select-Object script

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
Top PowerShell Commands for Beginners Select-Object script smaller

So you can see, there are many ways to work with object properies in a script.


Format-List/Table

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.

Top PowerShell Commands for Beginners Format-Table

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
Top PowerShell Commands for Beginners Format-List

Out-File

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
Top PowerShell Commands for Beginners Out-File

This command is simple and streightforward, but have many uses when you are scripting.


Export-Csv

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
Top PowerShell Commands for Beginners Export-csv

Now we can open the “Processes.csv” file in MS Excel.

Top PowerShell Commands for Beginners Export-csv excel file

Top Ten PowerShell Commands for Beginners and how to use them.

Wrap-up; TLDR

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.