Home » Uncategorized

Windows Powershell Commands for Beginners


  • Introduction of Powershell
  • Need of powershell
  • BackGround of Powershell
  • Tools
  • Why its better than alternatives ?
  • Top Most Administrative Powershell Commands
  • Working with Pipeline
  • Selecting, Sorting, Measuring, Exporting, Importing, Converting,     Filtering, Passing Data in Pipeline.
  • Using PSProviders and PSDrives
  • Formatting Output
  • Introduction of WMI (Windows Management Instrumentation)
  • Condition
  • Looping (For/ Eor-each/While)
  • Take user Input
  • Display Output

  • This Shell is Command line based or can be included to GUI.
  • Developed by Microsoft
  • Based on .Net Framework
  • It includes Scripting.
  • Capability to interact with another Windows based Softwares, for instances:-
  • Citrix, SQL, AD, any open APIs like Slack etc.

Designed by Jeffrey Snover, Bruce Payette, James Truher (et al.)
Developer Microsoft
First appeared November 14, 2006

Stable release 5.1.14393 / August 2, 2016; 8 months ago
Preview release 6.0.0 Alpha 17 / March 8, 2017; 35 days ago
Typing discipline Strong, safe, implicit and dynamic
Platform .NET Framework, .NET Core
OS Windows 7 and later, macOS, CentOS, Ubuntu

Filename extensions
•.ps1 (Script)
•.ps1xml (XML Document)
•.psc1 (Console File)
•.psd1 (Data File)
•.psm1 (Script Module)
•.pssc (Session Configuration File)
•.cdxml (Cmdlet Definition XML Document)

What’s Need ?   

  • Microsoft describes PowerShell as “a task-based command-line shell and scripting language… built on the .NET Framework.”  What is so great about PowerShell?  Why should you use it?
  • PowerShell is both a command-line shell and scripting language
  • PowerShell can interact with a dizzying number of technologies.
  • .NET Framework, the Registry, COM, WMI, ADSI.  Exchange, Sharepoint, Systems Center, Hyper-V, SQL.   VMware vCenter,Cisco UCS, Citrix XenApp and XenDesktop.  REST APIs, XML, CSV, JSON, websites, Excel and other Office applications.  C# and other languages, DLLs and other binaries, including *nix tools.
  • PowerShell is object-based.This gives us incredible flexibility.  Filter, sort, measure, group, compare or take other actions on objects as they pass through the pipeline.  Work with properties and methods rather than raw text.
  • Microsoft is putting its full weight behind PowerShell.PowerShell isn’t going away.  It is a requirement in the Microsoft Common Engineering Criteria, and a Server product cannot be shipped without a PowerShell interface.
  • In many cases, Microsoft is building their GUI with the help of Powershell only. Here we can perform more than what we think on GUI.
  • PowerShell also provides a hosting API with which the PowerShell runtime can be embedded inside other applications.


  • Every released version of Microsoft DOS and Microsoft Windows for personal computers has Shell, so till Windows9x, it was relying on Command.com and later on in NT family it came as cmd.exe
  • In 1998, MS launched Cscript.exe to allow compatible scripting languages like Jscript and VBScript.
  •  By 2002 Microsoft had started to develop a new approach to command line management, including a shell called Monad (also known as Microsoft Shell or MSH) 
  • PowerShell version 1 was released on September 26, 2006 , but officially released on Nov 14, 2006.       
  •   PowerShell v2.0 was completed and released to manufacturing in August 2009, as an integral part of Windows 7 and Windows Server 2008 R2
  • On 18 August 2016, Microsoft announced that they had made PowerShell open-source and cross-platform with support for Windows, OS X, CentOS and Ubuntu.
  • The move to open source created a second incarnation of PowerShell called “PowerShell Core”, which runs on .NET Core. It is distinct from “Windows PowerShell”, which runs on the full .NET Framework. Starting with version 5.1, PowerShell Core is bundled with Windows Server 2016 Nano Server.

Inbuilt Tools   
Windows Powershell Commands for Beginners

•Powershell is better than its legacy alternatives like :-VBScripting, Bash
•Consistent syntax, consistent command structure
•Future-proof through .NET integration
•Support through help and documentation (Get-Help)

Easy to find out any way, just clue is required. (Get-Command “*Service*” )

Windows Powershell Commands for Beginners
Windows Powershell Commands for Beginners
Top-Most Used
  1.  Navigate the Windows Registry like the file system:cd hkcu:
  2.  Search recursively for a certain string within files:dir –r | select string “searchforthis”
  3.  Find the five processes using the most memory:ps | sort –p ws | select –last 5
  4.  Cycle a service (stop, and then restart it) like DHCP:Restart-Service DHCP
  5.  List all items within a folder:Get-ChildItem – Force
  6.  Recurse over a series of directories or folders:Get-ChildItem –Force c:\directory –Recurse
  7. Remove all files within a directory without being prompted for each:Remove-Item C:\tobedeleted –Recurse
  8. Restart the current computer:(Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).Win32Shutdown(2)

Collecting information
  •  Get information about the make and model of a computer:Get-WmiObject -Class Win32_ComputerSystem
  •  Get information about the BIOS of the current computer:Get-WmiObject -Class Win32_BIOS -ComputerName .
  • List installed hotfixes — QFEs, or Windows Update files:Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName .Get the username of the person currently logged on to a computer:
  • Get-WmiObject -Class Win32_ComputerSystem -Property UserName -ComputerName .
  • Find just the names of installed applications on the current computer:
  • Get-WmiObject -Class Win32_Product -ComputerName . | Format-Wide -Column 1
  • Get IP addresses assigned to the current computer:Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Format-Table -Property IPAddress
  •  Get a more detailed IP configuration report for the current machine:Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS*
  •  Find network cards with DHCP enabled on the current computer:Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter “DHCPEnabled=true” -ComputerName .
  •  Enable DHCP on all network adapters on the current computer:
  • Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=true -ComputerName . | ForEach-Object -Process {$_.EnableDHCP()}

Software management

  • Install an MSI package on a remote computer:(Get-WMIObject -ComputerName TARGETMACHINE -List | Where-Object -FilterScript {$_.Name -eq “Win32_Product”}).Install(\\MACHINEWHEREMSIRESIDES\path\package.msi)
  • Upgrade an installed application with an MSI-based application upgrade package:
  • (Get-WmiObject -Class Win32_Product -ComputerName . -Filter “Name=’name_of_app_to_be_upgraded'”).Upgrade(\\MACHINEWHEREMSIRESIDES\path\upgrade_package.msi)
  •  Remove an MSI package from the current computer:(Get-WmiObject -Class Win32_Product -Filter “Name=’product_to_remove'” -ComputerName . ).Uninstall()

Machine management

  • Remotely shut down another machine after one minute:Start-Sleep 60; Restart-Computer –Force –ComputerName TARGETMACHINE
  •  Add a printer:(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection(“\\printerserver\hplaser3”)
  • Remove a printer:(New-Object -ComObject WScript.Network).RemovePrinterConnection(“\\printerserver\hplaser3 “)
  • Enter into a remote PowerShell session — you must have remote management enabled:enter-pssession TARGETMACHINE
  • Use the PowerShell invoke command to run a script on a remote servers:invoke-command -computername machine1, machine2 -filepath c:\Script\script.ps1

Multiple Ways to perform One Task 

Shutdown a computer
Stop-Computer “ComputerName1, Name2”Stop-Computer –computer DC1 –Credential nwtraders\administrator
Shutdown –i (to restart/shutdown bulk of computer in one shot with message)
Restart-Computer “ComputerName1”
Get-wmiobject –class WIN32_OperatingSystem –ComputerName .).Invokemethod(“Win32Shutdown”,0)Or (gwmi Win32_OperatingSystem).Win32Shutdown(0).
1.Log Off   “0”
2.Forced Log Off   “4”
3.Shutdown “1”
4.Forced Shutdown “5”
5.Reboot  “2”
6.ForcedReboot  “6”
7.Power Off  “8”
8.Forced Power Off  “12”
Concept of Pipeline ( | )
  • Pipelining could almost be described as PowerShell’s signature tune.
  • Piping work almost everywhere in Powershell
  • PowerShell does not pipe text between commands. Instead, it pipes objects.
  • Piping is used for several purposes like:- got a focused result, use output of previous command to further within same line,filtering.
  • PowerShell encourages you to join two statements so that the output of the first clause, becomes the input of the second clause.Example :-  Get-Process
Windows Powershell Commands for Beginners
Select or Select-Object Get-process | Select ID,ProcessName
Windows Powershell Commands for Beginners
  • Sort-Object
Windows Powershell Commands for Beginners
  • Measure-Object / Count
Windows Powershell Commands for Beginners
Exporting/ Converting
  • Export-csv/
Windows Powershell Commands for Beginners
  • Get-childitem > c:\test1.txt
  • Get-childitem | Out-File c:\test2.txt 
  • ConvertTo-Html
Windows Powershell Commands for Beginners

ConvertTo-SecureString :- Convert any string into Encrypted form

-AsSecureString :- Take input from user in secure way

$var = Read-Host -AsSecureString

$var1 = ConvertTo-SecureString -SecureString $var


  • Where-Object
Windows Powershell Commands for Beginners
Windows Powershell Commands for Beginners
Formatting Output
Windows Powershell Commands for Beginners
Windows Powershell Commands for Beginners
Variable ($)
  • $var = (get-process).count
  • Windows PowerShell works with objects. Windows PowerShell lets you create variables – essentially named objects – to preserve output to use later. If you are used to working with variables in other shells, remember that Windows PowerShell variables are objects, not text.
  • Variables are always specified with the initial character $, and can include any alphanumeric characters or the underscore in their names.

IF / IF-Else

$var = (get-process).countif($var -gt 110)
Write-Host “We have $var process in running state “
Else {
Write-Host “Number of Processes are less than 100 in count.”
Windows Powershell Commands for Beginners
Read User Input/Display Output
• $var =Read-Host –prompt “Enter your Name:”
Windows Powershell Commands for Beginners
Display output:-  Write-Host / Echo / Write-Output
Windows Powershell Commands for Beginners

Write-Host is having more attributes

Windows Powershell Commands for Beginners

Write-Host –NoNewLine “Counting from 1 to 9 (in seconds):  “


foreach($element in 1..9){


  Write-Host –NoNewLine  “${element} “


  Start-Sleep –Seconds 1




Write-Host “”

Output :-  Counting from 1 to 9 (in seconds):  1 2 3 4 5 6 7 8 9 


Write-Output should be used when you want to send data on in the pipe line, but not necessarily want to display it on screen.

PS C:\> Write-Output”test output” | Get-Member

This command pipes the “test output” string to the Get-Member cmdlet, which displays the members of the System.String class, demonstrating that the string was passed along the pipeline.

 Windows Powershell Commands for Beginners


  • Looping are required to perform a repeated set of operations for number of times.
  • These above mentioned are three major loop techniques:
  1. For Loop
  2. For-each Loop
  3. While Loop
  4. Do While

 For / For-Each  Loop 
Windows Powershell Commands for Beginners

ForEach is specially used to fetch elements from an array

Windows Powershell Commands for Beginners

While/ Do-While Loop 

While :-  As long as the condition remains true, PowerShell reruns the {command_block} section.

while($val -ne 10) { $val++ Write-Host $val }

Windows Powershell Commands for Beginners

Do-While:- First the Command Block will run and then it will check condition

Do {

$val++ Write-Host $val

} while($val -ne 10)


Get-Content :- To Read content from a file (.txt/ .csv /.xlsx)
$var = Get-Content -Path .\Desktop\names.txt
foreach ($i in $var)
Write-Host $i
Start-Sleep -Seconds 1

Windows Powershell Commands for Beginners

  • Import-csv :- To Read Data from csv file $var = Import-Csv -Path .\Desktop\names.csv