Black Friday and Cyber Monday! I have listed the best Smart Home Deals for you to expand your smart home. Read more here

Top 10 PowerShell Commands that you Must Know

PowerShell is really powerful and can make your daily work a lot easier. With all the modules that you can load, there are a lot of PowerShell commands. But what are the PowerShell commands that you must know?

Some of the commands you might already know, but did you know you can get a full autocomplete of all possible parameters with a key combination? Or search through the commands that you entered earlier?

In this article, I have collected the top 10 PowerShell commands that will really help you in your daily work. If have you any other recommendations please add them in the comments below!

Top 10 PowerShell Commands

Below you will find the most useful PowerShell commands that can make your daily work in PowerShell much easier.

Get-Member

When writing scripts you sometimes want to know which properties and methods are returned by a cmdlet. One way to do this is to run the cmdlet and go through all the returned properties. But a more convenient way is to use the Get-Member cmdlet.

When you pipe the cmdlet behind a command it will list all the properties, methods, and definitions of each. For example, to get all properties and methods from the Get-EXOMailbox cmdlet we can do:

Get-EXOMailbox | Get-Member
PowerShell Commands
Get-Member

Get-History

When working in a CLI, like PowerShell, you probably often use the up arrow key to find that one command that you have used earlier. Now the longer you have been working in your PowerShell session, the more commands you might have to go through.

But did you know that you can use the Get-History cmdlet to view all commands that you have used? PowerShell keeps track of commands that you entered during a session. By default, it will remember the last 4096 entries!

Just type Get-History to view all the last commands that you have used:

Get-History
Get History command
Get-History

In the example, I only have 8 entries, so it’s easy to find the command you are looking for. But when you have more entries, then it’s good to know that we can also search/filter the Get-History cmdlet, by piping a Where-object to it:

Get-History | Where-Object {$_.Commandline -like "*EXOMailbox*"}

# Result
  Id CommandLine
  -- -----------
   4 Get-EXOMailbox -Filter "RecipientType -eq 'UserMailbox'"
   6 Get-EXOMailbox -Filter "RecipientType -eq 'UserMailbox'"
   7 Get-EXOMailbox -Filter "RecipientType -eq 'UserMailbox'" | Select Name,EmailAddresses

Get-Random

When writing scripts it’s not uncommon to use a random function to generate a random integer. But the Get-Random cmdlet in PowerShell can do more than just generate an integer. It’s also capable of selecting a random entry from an array for example.

The Get-Random cmdlet uses the RandomNumberGenerator class which allows it to generate cryptographically secure randomness. It returns by default an integer between 0 and 2,147,483,647. But of course, we specify a minimum and maximum value.

Get-Random -Minimum -50 -Maximum 150

# Result
111

Get Random can also be used to select a random item from an array:

$array = 'apple','pear','raspberry','kiwi','banana','melon','blueberry'

$array | Get-Random

# Result
raspberry

Ctrl + Space

This is not really a PowerShell command, but you wish that you knew about this key combination earlier. When using a cmdlet we all often type the hyphen - and then tab through all the possible parameters that cmdlet has.

But did you know you can view all parameters at once? To try this out, type a cmdlet and press Ctrl + Space:

best powershell commands
Display all nouns and parameters

It will display all nouns and parameters that you can use with the cmdlet. Now pressing Ctrl + Space is a method you will need to get used to, but you can also set the following in your PowerShell profile:

Set-PSReadlineKeyHandler -Key Tab -Function Complete

This way you will only need to type the hyphen and press Tab twice to list all parameters.

Enter-PSSession

Need to run a PowerShell command on the server? Most open a remote desktop, to start a PowerShell session on the server, but there is really no need for that. With PowerShell, we can start an interactive session with a remote computer using the Enter-PSSession cmdlet.

To start an interactive session, simply type:

Enter-PSSession -Computername LazySrvLab02

# Result
[LazySrvLab02]: PS C:\>

You can now run PowerShell commands on the server, just as if you are working directly on the server. If you need to authenticate with different credentials, then use the parameter -credential to specify the username that you want to use. You will be prompted for the password.

To exit the remote session, type Exit-PSSession

Paste Output to Clipboard

When retrieving data with PowerShell we have a couple of options when it comes to outputting the data. We can view the results in the console, grid view, or export them to CSV for example. But did you know you can also output the results directly to your clipboard?

Simply pipe clip behind your command to store the results on your clipboard:

Get-Content -path "c:\temp\fruits.txt" | clip

You can now simply paste the contents into any program that you want. To view/paste the contents of the clipboard in PowerShell use Get-Clipboard:

Get-Clipboard

# Result
BlueBerry;StrawBerry;BlackBerry;RaspBerry;CranBerry;
Copy PowerShell output to clip board
clip command in PowerShell

Out-Gridview

When testing scripts or working in PowerShell we often output the result directly in the console. We can format the result set as a table with format-table or in short ft. But with large data sets, this isn’t always the best option. Data may be truncated or columns are missing in your overview.

format table
Default table output in PowerShell

The grid view output is a great solution to that. It will output all the data in a nice searchable, sortable view in a new window. The table contains all columns, which you can sort and search through.

Get-EXOMailbox | Out-Gridview
out-gridview
PowerShell Out-Gridview

In the top left corner, you can add filters to the result set, allowing you to easily find the correct data that you need:

filter grid view
Grid-view filter

Now the best part of the gridview in PowerShell, we can select the items we need and pass them back to PowerShell. For this, you will need to add the parameter -PassThru to the cmdlet:

Get-EXOMailbox | Out-Gridview -PassThru

Select the records that you need and press Ok. You can just pipe a select behind it, or store the results in a variable.

result

Export-CSV

The Export-CSV command is also one of those commands that is great to use when you are retrieving data with PowerShell. We all know that Excel is great when it comes to processing data further. The Export-CSV command generates a CSV file from your data.

Add the NoTypeInformation and Encoding parameter behind it. The first will remove the header information in your CSV file and the latter makes sure that the right encoding is used.

Read more about Export-CSV in this article.

Get-EXOMailbox | Export-CSV -path c:\temp\mailbox.csv -NoTypeInformation -Encoding UTF8

Set-StrictMode

The Set-StrictMode command in PowerShell establishes and enforces coding rules. By default, strict mode is turned off and that can cause a real mess. When strict mode is off, PowerShell threats uninitialized variables as $null or 0, for example.

Note the following code:

$username = 'adelev'
Get-EXOMailbox $usename | Set-mailbox -Hiddenfromaddresslist:$true

In the example above, I have misspelled the $username variable in the Get-ExoMailbox command. If Strict Mode is not enabled, PowerShell will handle the misspelled variable $usename as a $null, with the result that the cmdlet will get all mailboxes and hide them all from the address list.

When you set StrictMode to version 2, PowerShell will instead throw an error that the variable is not set.

Set-StrictMode -Version 2
Top PowerShell Commands
Strict Mode set

I recommend adding the Set-StrictMode -Version 2 command to your PowerShell Profile.

Test-NetConnection

We pretty much all know the ping command, an easy and straightforward command to test if a network device is online, and what the latency is for example. But there is also a PowerShell variant, which is a bit more powerful.

The Test-NetConnection cmdlet not only allows you to ping a device, but we can also specify the port to test. This is great when you want to test if applications can be accessed or determine firewall rule problems.

For example, if you want to know if you can access the SMTP server of Office 365 you can do the following:

Test-NetConnection -Computername smtp.office365.com -port 587

# Result
ComputerName     : smtp.office365.com
RemoteAddress    : 52.97.144.178
RemotePort       : 587
InterfaceAlias   : Wi-Fi
SourceAddress    : 192.168.1.22
TcpTestSucceeded : True

Read more about Test-NetConnection in this article.

Wrapping Up

With PowerShell, we can always learn new things, and improve our scripts or work methods, to make our work easier. These PowerShell commands can really help you with that. Commands like Set-StrictMode and the full autocomplete function are one of the best commands to know and use.

I hope you found this top 10 list useful, if you have any questions or suggestions, please drop a comment below!

Get more stuff like this

IT, Office365, Smart Home, PowerShell and Blogging Tips

I hate spam to, so you can unsubscribe at any time.

1 thought on “Top 10 PowerShell Commands that you Must Know”

Leave a Comment

0 Shares
Tweet
Pin
Share
Share