Skip to content

Using Powershell

Powershell has Verb-Noun structure:

Get- # get something
Start- # run something
Out- # output someting
Stop- # stop something running
Set- # define something
New- # create something

Get-Help is a useful command (gets help on argument).

Creating a Directory

New-Item -Path 'C:\Users\user\folder' -ItemType Directory

Execution Policies

Powershell has policies for running scripts:

  • Restricted: No scripts are allowed. This is the default setting, so it will display first time when you run the command.
  • AllSigned: You can run scripts signed by a trusted developer. With the help of this setting, a script will ask for confirmation that you want to run it before executing.
  • RemoteSigned: You can run your or scripts signed by a trusted developer.
  • Unrestricted: You can run any script which you wants to run

Execution policies can be get and set:

Get-ExecutionPolicy
Set-ExecutionPolicy

Writing to Terminal

Write-Host "Hello world"

Piping Output From One Command To Another

Get-WmiObject -Class Win32_OperatingSystem ComputerName localhost |
Select-Object -Property CSName,FreeVirtualMemory

Special Variables

  • Error : An array of error objects which display the most recent errors
  • $Host : Display the name of the current hosting application
  • $Profile : Stores entire path of a user profile for the default shell
  • $PID : Stores the process identifier
  • $PSUICulture : It holds the name of the current UI culture.
  • $NULL : Contains empty or NULL value.
  • $False : Contains FALSE value
  • $True : Contains TRUE value

Key Concepts

  • Cmdlets Cmdlet are build-command written in .NET languages like VB or C#. It allows developers to extend the set of cmdlets by loading and write PowerShell snap-ins.
  • Functions Functions are commands which is written in the PowerShell language. It can be developed without using other IDE like Visual Studio and devs.
  • Scripts Scripts are text files on disk with a .ps1 extension
  • Applications Applications are existing windows programs.
  • What if Tells the cmdlet not to execute, but to tell you what would happen if the cmdlet were to run.
  • Confirm Instruct the cmdlet to prompt before executing the command.
  • Verbose Gives a higher level of detail.
  • Debug Instructs the cmdlet to provide debugging information.
  • ErrorAction Instructs the cmdlet to perform a specific action when an error occurs. Allowed actions continue, stop, silently- continue and inquire.
  • ErrorVariable It specifies the variable which holds error information.
  • OutVariable Tells the cmdlet to use a specific variable to hold the output information
  • OutBuffer Instructs the cmdlet to hold the specific number of objects before calling the next cmdlet in the pipeline
  • Cmdlets Build-commands written in .NET languages such as VB and C#