PS: Intro Commands

3 minute read

Description:

These are entry level Powershell Commands and how the language works:

NOTE: Almost all command prompt commands work just as well in Powershell. Search the Scripting-CMD label to see examples of them.

Syntax

  1. Cmdlets are small scripts that follow a dash-separated verb-noun convention such as Get-Process. Similar Verbs with Different Actions:

    • New- Creates a new resource
    • Set- Modifies an existing resource
    • Get- Retrieves an existing resource
    • Read- Gets information from a source, such as a file
    • Find- Used to look for an object
    • Search- Used to create a reference to a resource
    • Start- (asynchronous) begin an operation, such as starting a process
    • Invoke- (synchronous) perform an operation such as running a command
  2. Parameters: Each verb-noun named cmdlet may have many parameters to control cmdlet functionality.

  3. Objects: The output of most cmdlets are objects that can be passed to other cmdlets and further acted upon. This becomes important in pipelining cmdlets.

Useful Cmdlets (and aliases)

To Get a directory listing (ls, dir, gci):

1
   Get-ChildItem

To Copy a file (cp, copy, cpi):

1
   Copy-Item src.txt dst.txt

To Move a file (mv, move, mi):

1
   Move-Item src.txt dst.txt

To Find text within a file:

1
2
   Select-String path c:users*.txt pattern password
   ls -r c:users -file | % {Select-String -path $_ -pattern password}

To Display file contents (cat, type, gc):

1
   Get-Content file.txt

To Get present directory (pwd, gl):

1
   Get-Location

To Get a process listing (ps, gps):

1
   Get-Process

To Get a service listing:

1
   Get-Service

To Format output of a command (Format-List):

1
   ls | Format-List property name

To Paginating output:

1
   ls r | Out-Host -paging

To Get the SHA1 hash of a file:

1
   Get-FileHash -Algorithm SHA1 file.txt

To Export output to CSV:

1
   Get-Process | Export-Csv procs.csv

Pipelining, Loops, and Variables

To Pipe A cmdlet’s output to another cmdlet:

1
   Get-Process | Format-List property name

To Use ForEach-Object in the pipeline (alias %):

1
   ls *.txt | ForEach-Object {cat $_}

To Use Where-Object condition (alias where or ?):

1
   Get-Process | Where-Object {$_.name eq "notepad"}

To Generating ranges of numbers and looping:

1
2
   1..10 | ForEach-Object{ New-Item -Name "$psitem.txt" -ItemType File} # creates 10 text files
   1..10 | % {echo "Hello!"} # writes "Hello!" 10 times

To Creating and list variables:

1
2
   $tmol = 42
   ls variable:

To See An Example of passing cmdlet output down pipeline:

1
2
   dir | group extension | sort
   Get-Service dhcp | Stop-Service -PassThru | Set-Service -StartupType Disabled

To Give A Choice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   $OUPathValidate = $true
   While ($OUPathValidate) 
   {
      $OUPath = Read-Host 'INTERN/EXTERN'
      If ($OUPath.ToUpper() -eq 'INTERN') 
      {
         $OUPath = 'OU=Intern,OU=Users,OU=Zero,DC=int,DC=zero,DC=com'
         $OUPathValidate = $false
      } 
      ElseIf ($OUPath.ToUpper() -eq 'EXTERN')
      {
         $OUPath = 'OU=Extern,OU=Users,OU=Zero,DC=int,DC=zero,DC=com'
         $OUPathValidate = $false
      }
      Else 
      {
         Write-Host 'That is not a valid input, please use INTERN or EXTERN'
      }
   }

To Trap Errors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   # Run your code with the ErrorVariable switch. Remember to just use a string as the name, no $ required
   Get-ChildItem $path | Move-Item -Destination $newlocation -ErrorVariable fileErrors

   # Your exceptions are now here
   $fileErrors

   # or:
   [System.Collections.ArrayList]$MoveItemErrors = @()
   $ItemsToMove = Get-ChildItem $path
   foreach ($item in $ItemsToMove) {
      try {
         $item | Move-item -Destination $newlocation -ErrorAction Stop
      }
      catch {
         $null = $MoveItemErrors.Add($_)
      }
   }

To Create A Hashtable:

1
2
3
4
5
6
7
   $Params = @{
      'Path' = "HKCU:\Some\Path"
      'Name' = "Category"
      'Value' = "1" 
      'Type' = "DWORD"
   }
   SetReg @Params | Out-Null

To Use Arrays => Use lists instead where appropriate

1
2
3
4
5
   $Array = [System.Collections.Generic.List[PSObject]]@()
   foreach ($I in $Items) 
   { 
   [void]$Array.Add($Item) 
   }

To See If A Switch Was Used

1
   If ([Bool]($MyInvocation.BoundParameters.Keys -match 'Recurse')) { # Parameter 'Recurse' was used } Else { # Parameter 'Recurse' was NOT used }

To Learn to Use #Requires Statements

1
2
3
4
5
6
   #Requires -Version <N>[.<n>]
   #Requires -PSSnapin <PSSnapin-Name> [-Version <N>[.<n>]]
   #Requires -Modules { <Module-Name> | <Hashtable> }
   #Requires -PSEdition <PSEdition-Name>
   #Requires -ShellId <ShellId>
   #Requires -RunAsAdministrator

To Pause A Script

1
2
3
4
5
6
7
   Function Stop-Script 
   {
   Write-Output "Press Any Key To Continue ..."
   $Host.Ui.Rawui.Readkey("Noecho,Includekeydown") | Out-Null
   }
   # or
   cmd /c "pause"

To Set Param Blocks

1
2
3
4
5
6
7
8
9
10
11
   # If in PS ISE, just use Ctrl+J!!
   # If not, something like
   [Cmdletbinding()]
      Param
      (
         [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
         [String[]]$RegistryKey,
         
         [Parameter(Mandatory=$true, Position=1)]
         [String]$Username
      )

Comments