PowerShell Fundamentals

This post explains the basics of PowerShell and servers as an introduction to the shell.
The following sites also provide usefull recources:

https://learn.microsoft.com/

https://ss64.com/ps/

About PowerShell

PowerShell is a command line shell and scripting language made by Microsoft.
It provides us with task automation and configuration management using commands and scripts.
Unlike most shell languages Powershell mainly uses objects instead of plain-text(strings). These objects then consist of properties.

PowerShell 5 is build into Windows, while PowerShell 7 can be downloaded of the internet. PowerShell 7 uses .NET Core and can be used on MacOS & Linux.

Powershell can perform commands like the cmd and use its own commands called cmdlets. cmdlets usually consist of a verb followed by a noun(verb-noun).

The following verbs can be used:
Get — To get something
Start — To run something
Out — To output something
Stop — To stop something that is running
Set — To define something
New — To create something


Tips:
You can use TAB as autocomplete and CTRL + SPACE to get an autocomplete menu.

Doing Get-ChildItem -[CTRL + SPACE] will reveal all the available parameters for Get-ChildItem.

Getting help in the Shell

Before we dive further into PowerShell it is important to know how to get help form within the Shell. In theory you dont have to google, you can find cmdlets and their behaiviour within the Shell.


The following cmdlets can be used to get help:

get-help [cmdlet]
get-help [cmdlet] -full
get-help [cmlet] -online
help [cmdlet]
Update the get-help definitions:
Update-Help

Example:



The following cmdlet can be used to find cmdlets:

get-commandget-command *printer*
get-command -verb "get" -noun "DNS"
get-command -Module vmware* -Verb get -Noun *snap*

Example 1:

Example 2:

The following cmdlet can be used to find an alias:

get-alias [alias]
get-alias -definition [cmdlet]

Navigating the Shell

Here are a few cmdlets used for navigation:

Change location:
set-location
cd

Change location up one folder: 
set-location ..
cd ..

List items in directory:
get-childitem
ls

List items and subfolders: 
get-childitem -recurse
ls -recurse

Delete the specified item/folder: 
remove-item
rm

Copy and item: 
copy-item
cp

Open item using default app: 
invoke-item
ii

Open current directory in win explorer: 
invoke-item .
ii .

Get-Childitem examples

Get only the directories in the current location:
Get-ChildItem -Directory

Get only the files in the current location: 
Get-ChildItem -File

Get items with the .dat extension: 
Get-ChildItem *.dat

Get items the start with "m":
Get-ChildItem m*

Get items in folder/subfolder and hidden items:
Get-ChildItem -Recurse -Force

Pipeline |

With a pipeline we can take the output of one cmdlet, and use it in another cmdlet.
This lets us shape the object to our desire.
In te example underneath i use a pipeline and the Select-Object to only show the Name & LastAccessTime of the objects from Get-Childitem.

Where-Object

With Where-Object, we can filter the output.
In the following example i use Where-Object to filter the output and only show objects that contain the string “down” in the name.

Variables

Variables allows us to store information(such as the result of cmdlets) into a single unit.
They always start with a dollar sign “$”.
Below an example:

We store the cmdlet Get-Childitem into a variable.

There are different types of variables:
Integer: [int]$number = 8
String: [string]$words = “Hello”
Array: [array]$array = 1,2,3

Another way to make an array is with @()
Example: $array = @(1, 2, 3)

Get-Member

With Get-Member(alias: gm) we can see properties/methodes of an object.

One of the methods for example is “Kill”.
We can kill the chrome process by enclosing it with () and perform the method on it:
(Get-Process -Name chrome).Kill()

We can also store the cmdlet in a variable and perform methods on it like this:

$ChromeProcess = Get-Process -Name Chrome
$ChromeProcess.Kill()


Modules

Modules can be installed to expand PowerShell with more cmdlets etc… Modules can be found on the internet in repositories.

List modules imported in current session:
Get-Module

List modules available to import into the session:
Get-Module -ListAvailable

Get the modules installed with PowerShellGet:
Get-InstalledModule

Import module into current session:
Import-Module

Remove a module from current session(does not uninstall):
Remove-Module

Find a module:
Find-Module

Find a module starting with "vmware" in the name:
Find-Module vmware*

See more info about a found module:
Find-Module vmware.powercli | fl

Install a module(you can use -name as a parameter):
Install-Module


Uninstall modules starting with "Az." in the name:
Get-InstalledModule Az.* | Uninstall-Module

See the cmdlets from a module:
Get-Command -Module WindowsUpdate

-Passthru

If a cmdlet does not return the result in the shell you can use the -PassThru parameter:

-WhatIf

If you are not sure about a piece of code/cmdlets, you can use -WhatIf to check if its safe.

You can also set the automtic variable $whatifpreference = ‘True’.
$whatifpreference is false by default but by setting it to True, it will be performed whenever possible.

Execution Policies

Get-ExecutionPolicy
Set-ExecutionPolicy

AllSigned:
Requires that all scripts and configuration files are signed by a trusted publisher, including scripts written on the local computer.

Bypass:
Nothing is blocked and there are no warnings or prompts.
Default. Sets the default execution policy. Restricted for Windows clients or RemoteSigned for Windows servers.

RemoteSigned:
Requires that all scripts and configuration files downloaded from the Internet are signed by a trusted publisher. The default execution policy for Windows server computers.

Restricted:
Doesn’t load configuration files or run scripts. The default execution policy for Windows client computers.

Undefined:
No execution policy is set for the scope. Removes an assigned execution policy from a scope that is not set by a Group Policy. If the execution policy in all scopes is Undefined, the effective execution policy is Restricted.

Unrestricted:
Beginning in PowerShell 6.0, this is the default execution policy for non-Windows computers and can’t be changed. Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the internet, you’re prompted for permission before it runs.

Powershell jobs

We can start script blocks as jobs and keep track of the progress.

Start-Job
Get-Job
Stop-Job
Remove-Job
Receive-Job

Example:



Leave a Reply

Your email address will not be published. Required fields are marked *