8 Quick and easy tips to get you started with PowerShell

Updated: Sep 11

PowerShell is a very powerful scripting language. It is built on top of the .NET framework and can interact with all its libraries. This architecture makes it a convenient tool for anyone working on Windows until recently, that is. Recently PowerShell was released for MacOS and Linux, making it an even more useful skill to possess.


If you are reading this, you are probably just getting started with PowerShell and wondering what you should learn first. Well, look no further. In this post, I will share with you the basic knowledge you need to get started with PowerShell.


Let's get started!

1. <verb>-<noun>

Like any spoken language, in PowerShell, you need to start by learning about Verbs and Nouns. PowerShell has "strict" rules about naming cmdlets (no typo), a.k.a. functions, ish. Core cmdlets always follow the format <verb>-<noun>. You can also think of it as <action>-<target>. Learning some of the basic verbs and their associated actions will help you find answers for what you are trying to do much more easily. You can find a list of approved verbs here. If you want a list of approved verbs directly into your PowerShell console, type:

Get-Verb

2. CTRL+SPACE autocomplete/search

One of the coolest things in PowerShell is its powerful autocomplete and search feature right from the command line. If you are unsure of the correct cmdlet, parameter, file, folder or variable to use, just use CTRL+SPACE and PowerShell will display all the available options to you can you can just select the one you want.



3. Get-Help

If you want to learn in more detail what options you have for using a cmdlet, enter:

Get-Help <cmdlet>

This will give you exhaustive documentation of the cmdlet. Added bonus, type 


Get-Help <cmdlet> -Examples 

to get actual examples on how to use the cmdlet.


4. Pipelines

Pipelines enable you to pass the results from one cmdlet to the next without storing the intermediate results. There is no limit to how many cmdlets can be chained together besides the compatibility between the output of one cmdlet and the input of the next. If a cmdlet returns a list of results, it will be passed down the chain one at a time. The current output is processed by the next item in the chain can be accessed using the built-in variable $_ or $PSItem.

#Get the list of files from C:\Temp
Get-ChildItem C:\Temp |
#Filter the previous list to only have files older than 3 days
Where-Object {$PSItem.LastWriteTime -lt (Get-Date).addDays(-3)} |
#Copy files older than 3 Days to C:\Archive and return the new
#files
Copy-item -destination C:\Archive -PassThru |
#Output the list of moved files in the new folder showing their
#full path (FullName) and the last time they were written to
Format-Table -Property FullName, LastWriteTime



5. Storing results

To store results of cmdlets, PowerShell, just like the vast majority of programming and scripting languages, uses variables. Variables start with $ and results are assigned as follow: 


$MyVariable = <My command>


6. Everything is an Object

This is an important differentiating factor from regular command lines. When a cmdlet returns a result, unless nothing is returned, it is always an object. This means that the result has properties and methods that can be accessed. You can access a list of properties and methods available for an object by piping (#4) the results of a cmdlet to the Get-Member cmdlet. To access a property, use the following notation 

$MyObject.MyProperty

7. ForEach-Object

ForEach-Object is one of the main ways in PowerShell to iterate over a collection of objects. You normally use a pipeline to feed objects into the cmdlet and access the currently processed object using the $_ variable. The syntax for ForEach-Object is:

 <My Data/Cmdlet> | ForEach-Object {<PowerShell code to execute for one item of the input data>}

For example:

#Get all the files in C:\Windows\Temp
Get-ChildItem C:\Windows\Temp |
ForEach-Object { #Process all the files
    #If a file size is greater than 1,000 bytes
    if($_.length -gt 1000) 
    {
        Remove-Item $_.Fullname #Delete the file
    }
}

8. Where-Object

Where-Object is a very useful PowerShell cmdlet because it allows you to filter data. It takes data in, usually through a pipeline, and returns a data list filtered to your specification. Its syntax is similar to ForEach-Object:

<My Data/Cmdlet> | Where-Object {<Filter condition>} 

Just like ForEach-Object, Where-Object processed the input data one at a time, and the current element is accessed using the built-in variable $_:

Get-ChildItem C:\Windows\Temp | Where-Object {$_.length -gt 1000}

There you have it! 8 very quick and basic tips to get you started on your PowerShell journey. Do you have any other tips? Let me know in the comments below. 



2,552 views

©2019-2020 by Bulles Technologies. Proudly created with Wix.com