Post

PowerShell - Posts

PowerShell Scripting

  • PowerShell is the Windows Scripting Language and shell environment built using the .NET framework.
  • Most PowerShell commands, called cmdlets, are written in .NET.
  • Unlike other scripting languages and shell environments, the output of these cmdlets are objects - making PowerShell somewhat object-oriented.
  • The normal format of a cmdlet is represented using Verb-Noun; for example, the cmdlet to list commands is called **Get-Command**
  • The most important 2 commands are **Get-Help** and Get-Command
  • Get-Help <CmdletName> -Online displays the online documentation for the specified cmdlet or topic.
  • PowerShell scripts usually have the .ps1 file extension.
1
2
3
4
5
6
7
8
#Common verbs to use include:
Get
Start
Stop 
Read
Write
New
Out
Command aliasCmdlet nameDescription of commandExamples        
shcmShow-CommandCreates Windows PowerShell commands in a graphical command window.         
acAdd-ContentAppends content, such as words or data, to a file.Add-Content -Path “C:\dir\test.txt” -Value “written in powershell”        
cat, gc, typeGet-ContentGets the contents of a file.Get-Content -Path “C:\Users\UN\Desktop\test.txt”        
scSet-ContentReplaces the contents of a file with contents that you specify.Set-Content .\test.txt “new content”        
echo, writeWrite-OutputSends the specified objects to the next command in the pipeline. If the command is the last command in the pipeline, the objects are displayed in the console.Write-Output “This is some text.”        
cd, chdirSet-LocationSets the current working location to a specified location.Set-Location -Path “C:\DirectoryName”        
gl, pwdGet-LocationGets information about the current working location or a location stack.Get-Location -Provider Registry #provider that defines the location        
niNew-ItemCreates a new item file or folderNew-Item -Path “C:\path\to\newfile.txt” -ItemType “File”        
clcClear-ContentDeletes the contents of an item, but does not delete the item.Clear-Content -Path “C:\dir\test.txt” #test.txt still exists        
del, ri, rm, rmdir           
erase, rdRemove-ItemDeletes files and folders.Remove-Item -Path “C:\path\to\item”        
dir, gci , lsGet-ChildItemGets the files and folders in a file system drive.Get-ChildItem -Path “C:\MyFolder” -Hidden -Recurse #list directories and subdirectories -SortBY sort based on a specific criteria -Filter D* #list directories starts with D -System #get system files
flFormat-ListFormats the output as a list of properties in which each property appears on a new line.Get-Process -Name “explorer”Format-List -Property Name, Id, StartTime -GroupBy #group output by specific property     
fwFormat-WideFormats objects as a wide table that displays only one property of each object.Get-ProcessFormat-Wide -Property Name -Column 4 #format the output in a wide table format       
ftFormat-TableFormats the output as a table.Get-ProcessFormat-Table -Property Name, Id, CPU, WorkingSet       
measureMeasure-ObjectCalculates the numeric properties of objects, and the characters, words, and lines in string objects, such as files of text.@(75, 89, 62, 95, 82)Measure-Object -Minimum -Maximum       
ghy, hGet-HistoryGets a list of the commands entered during the current session.Get-History Get-History -Id 5 # get command by id Get-History -Count 10 #the last 10 commands    
clhyClear-HistoryDeletes entries from the command history.Clear-History Clear-History -Id 10 #clear a specific command with id 10      
clsClear-HostClears the display in the host program.Clear-Host        
copyCopy-ItemCopies an item from one location to another.Copy-Item -Path .\test.txt -Destination path\to\destination Copy-Item -Path “C:\Source*” -Destination “D:\Destination" -Exclude *.doc “ # exclude all doc files -Include *.txt #copy txt files only    
move, mi , mvMove-ItemMoves an item from one location to another.Move-Item -Path “C:\Source\ReadOnlyFile.txt” -Destination “D:\Destination” -Force #allows you to move a read-only file without any restrictions.        
renRename-ItemRenames an item in a Windows PowerShell provider namespace.Rename-Item -Path “C:\path\to\oldfile.txt” -NewName “newfile.txt” -Force #rename items that are read-only or hidden.      
selectSelect-ObjectSelects objects or object properties.Get-Command New-*Select-Object CommandType #You can also use the following flags to select particular information ⇒ -first : gets the first x object -last : gets the last x object -unique : shows the unique objects -skip : skips x objects 
groupGroup-ObjectGroups objects that contain the same value for specified properties.Get-ChildItem -Path “C:\Path\To\Directory”Group-Object -Property Extension       
guGet-UniqueReturns unique items from a sorted list.Get-ProcessSort-ObjectSelect-Object processnameGet-Unique -AsString     
gps , psGet-ProcessGets the processes that are running on the local computer or a remote computer.Get-Process -Name “explorer” #get process y name Get-Process -ComputerName “remotecomputer” #get process on remote device      
sapsStart-ProcessStarts one or more processes on the local computer.Start-Process notepad.exe Start-Process -FilePath “D:\path\to\program.exe”      
killStop-ProcessStops one or more running processes.Stop-Process -Name ProcessName Stop-Process -Id pid      
compare, diffCompare-ObjectCompares two sets of objects.Compare-Object -ReferenceObject (Get-Content -Path C:\die\SrcOjb.txt) -DifferenceObject (Get-Content -Path C:\dir\DestObj.txt)        
giGet-ItemGets files and folders.Get-Item -Path “C:\Example\Directory”        
curlInvoke-WebRequestGets content from a webpage on the Internet.Invoke-WebRequest -Uri “https://www.example.com/” -Headers specify HTTP headers in the request -Method specify HTTP method in request    
icmInvoke-CommandRuns commands on local and remote computers.Invoke-Command -ComputerName Server1 -ScriptBlock { Get-Process } -AsJob        
ihyInvoke-HistoryRuns commands from the session history.Invoke-History -Id 2 Invoke-History -Id 5 -ErrorAction “Stop” #stops if error happen while execution      
iiInvoke-ItemPerforms the default action on the specified item.Invoke-Item -Path “C:\Path\To\File.txt” #invoke (open) file.txt        
nalNew-AliasCreates a new alias.New-Alias -Name “ls” -Value “Get-ChildItem” #create alias ls for Get-ChildItem -Force #to allow alias creation even if an alias with the same name exist      
epalExport-AliasExports information about currently defined aliases to a file.Export-Alias -Path “C:\path\to\alias_definitions.txt”        
ipalImport-AliasImports an alias list from a file.Import-AliasFromFile -FilePath “C:\path\to\alias_definitions.txt”        
salSet-AliasCreates or changes an alias (alternate name) for a cmdlet or other command element in the current Windows PowerShell session.Set-Alias -Name “globalAlias” -Value Get-Command -Option AllScope #create a global alias accessible in all sessions        
ogvOut-GridViewSends output to an interactive table in a separate window.Get-ProcessOut-GridView -Title “Running Processes”       
galGet-AliasGets the aliases for the current session.Get-AliasExport-Csv -Path “C:\path\to\potput\alias_list.csv” #export the list of aliases to a csv file       
epcsvExport-CsvConverts objects into a series of comma-separated (CSV) strings and saves the strings in a CSV file.Get-AliasExport-Csv -Path “C:\path\to\potput\alias_list.csv”       
ipcsvImport-CsvCreates table-like custom objects from the items in a CSV file.Import-Csv -Path “C:\Users\MG\Desktop\task\alias_list.csv”Format-Table #format the output of csv in table form       
gdrGet-PSDriveGets drives in the current session.Get-PSDrive -Persist #list persistent drives -Name C , D #list by name      
gjbGet-JobGets Windows PowerShell background jobs that are running in the current session.Get-Job -State Running -Id #get job by id      
sajbStart-JobStarts a Windows PowerShell background job.Start-Job -ScriptBlock { Get-ServiceWhere-Object { $_.Status -eq ‘Running’ } }       
rjbRemove-JobDeletes a Windows PowerShell background job.Remove-Job -Id 1, 2, 3        
rujbResume-JobRestarts a suspended jobResume-Job -Job (Get-Job -Id 1)        
gpGet-ItemPropertyGets the properties of a specified item.Get-ItemProperty -Path “HKLM:\Software\SomeKey”        
cppCopy-ItemPropertyCopies a property and value from a specified location to another location.Copy-ItemProperty -Path “MyApplication” -Destination “HKLM:\Software\MyApplicationRev2” -Name “MyProperty”        
rpRemove-ItemPropertyremove a property from an item (usually a registry key or a file system item)Remove-ItemProperty -Path “HKLM:\Software\MyApp” -Name “ExampleProperty”        
spSet-ItemPropertymodify the attributes and values associated with an item.Set-ItemProperty -Path “C:\Path\To\File.txt” -Name “Attributes” -Value “ReadOnly”        
sasvStart-ServiceStarts one or more stopped services.Start-Service -Name “Spooler” -ComputerName “RemoteComputer” #start process on remote computer      
spsvStop-Servicestop one or more services on a local or remote computer.Stop-Service -Name “Spooler”, “W32Time”        
gsvGet-ServiceGets the services on a local or remote computer.Get-Service -Name “wuauserv”Format-List *       
gsnGet-PSSessionGets the Windows PowerShell sessions on local and remote computers.Get-PSSession -ComputerName “RemoteComputer”        
ipmoImport-ModuleAdds modules to the current session.Import-Module -Name “MyModule”        
isepowershell_ise.exeExplains how to use the PowerShell_ISE.exe command-line tool.powershell_ise.exe #(GUI) application provided by Microsoft for scripting and automating tasks using PowerShell.        
mount, ndrNew-PSDriveCreates temporary and persistent mapped network drives.New-PSDrive -Name “Z” -PSProvider FileSystem -Root “\Server\Share” -Persist -Credential $credential # root Specifies the root path or location of the drive , -Presist Indicates whether the drive should persist across PowerShell sessions        
rdrRemove-PSDriveDeletes temporary Windows PowerShell drives and disconnects mapped network drives.Remove-PSDrive -Name “Z”        
nsnNew-PSSessionCreates a persistent connection to a local or remote computer.New-PSSession -ComputerName “RemoteServer” -Credential $credential        
noneGet-EventLogretrieve entries from the Windows event logs on a local or remote computer.Get-EventLog -LogName “Application” -Newest 10 -UserName filter by username -MachineName #Specifies the name of the remote computer from which to retrieve event log entries    
noneGet-NetAdapterretrieve information about network adaptersGet-NetAdapter -Name “Ethernet”        
noneGet-NetIPAddressretrieve IP address informationGet-NetIPAddress -AddressFamily IPv4 Get-NetIPAddress -InterfaceAlias “Ethernet” #retrieve IP addresses associated with a specific network interface -IPAddress #IP addresses matching a specific IP    
noneGet-FileHashFind file hashGet-FileHash -Algorithm MD5-path path/to/file        
noneGEt-NetTCPConnectionused to retrieve information about active TCP connections on computerGet-NetTCPConnection -State Established        
gluGet-LocalUserretrieve information about local user accountsGet-LocalUser -Name “JohnDoe” Get-LocalUser -AccountNeverExpires Get-LocalUser -Group “Administrators” -SID # find user with his sid  
nluNew-LocalUserCreates a new local user account on the computer.New-LocalUser -Name “Username” -Password “Password”        
rluRemove-LocalUserDeletes a local user account from the computer.Remove-LocalUser -Name “Username”        
sluSet-LocalUserModifies properties of a local user accountSet-LocalUser -Name “Username” -PasswordNeverExpires $true        
glgGet-LocalGroupRetrieves information about local groups on the computer.Get-LocalGroup        
nlgNew-LocalGroupCreates a new local group on the computer.New-LocalGroup -Name “GroupName”        
algmAdd-LocalGroupMemberAdds a user or another group to a local group on the computer.Add-LocalGroupMember -Group “GroupName” -Member “Username”        
noneGet-ScheduleTaskretrieve information about scheduled tasksGet-ScheduleTask -TaskName new-sched-task -User “Administrator” #filter tasks by user -TriggerType Daily #filter by triagged type    
noneGet-Aclretrieve the access control list (ACL) of a file, folder, or other system objectsGet-Acl -Path “HKLM:\SOFTWARE\MyApp”        

PowerShell scripting

Commenting and Documentation

  • In PowerShell, comments are denoted by the # (hash) symbol.
  • Documentation can be provided as comments within your script or module, but it’s often more structured and included in separate files or formats like Markdown, HTML, or XML.
  • <# ... #> is commonly used for documentation in PowerShell scripts and functions.

**System Environment Variables**

  • Environment variables store data that’s used by the operating system and other programs.
  • You can access environment variables in PowerShell using the env: drive.
  • When you change environment variables in PowerShell, the change affects only the current session.
1
2
3
4
5
ls env:\                      #list all environment variables
echo $env:USERNAME            #print username
echo $env:windir              #echo path to the Windows directory.
$env:MyVariable = "MyValue"   #sets a new environment variable.
echo $env:Path                #specify directories where executable files are located.

**Variables and Data Types**

  • PowerShell infers the data type based on the assigned value.
  • Data Types: PowerShell supports various data types, including: Strings, Integers, Floats, Booleans, Arrays, Hashtable, Null.
  • Get variable type $variable.GetType()
  • PowerShell can automatically convert between data types when necessary, based on the context.
1
2
3
4
5
6
7
$result = 42

$name = "Alice"
$message = "Hello, $name!"  

$IsWorking = $true
Remove-Variable name         #name is removed 

**Arrays & Primitive Vs Non Primitive Data Types**

  • Primitive data types represent basic, single values and are the building blocks of more complex data structures (integers, boolean, double).
  • Non-primitive data types are more complex and can store multiple values(array, Hashtable, ArrayList, objects).
    • Array(@()) : An ordered collection of values, which can be of different data types.

      1
      2
      3
      4
      5
      6
      
        $myarray = "Alice" , "Jhon" , 20 
        $myarray[1]                          #John
        $myarray.Length                      #3
        $myArray += "US"                     #Array contains "Alice" , "Jhon" , 20 , US
        $myarray[0].EndsWith("e")            #True
        $myarray.Contains("US")              #True
      
    • Hashtable(@{}) : A collection of key-value pairs.

      1
      2
      3
      4
      5
      6
      
        $hash = @{ Number = 1; Shape = "Square"; Color = "Blue"}
        $hash["Size"] = 30                         #add Key Size with value 30
        $hash.Remove("Number")                     #remove key "Number"
        $hash.Count                                #number of key-value pairs => 3 (Number was removed)
        $hash.ContainsKey("Shape")                 #check if it contains a key called Shape => True
        $hash.ContainsValue("Green")               #check if it contains a value Green => False
      
    • ArrayList : A dynamic array that can grow or shrink in size.

      1
      2
      3
      4
      5
      6
      
        $myArrayList = New-Object System.Collections.ArrayList     #create an ArrayList
        $myArrayList.Add("EG")                                     # arraylist contains EG
        $myArrayList.Insert(0, "SU")                               #SU inserted at index 0 => SU , EG
        $myArrayList.Sort()                                        #sort arraylist => EG , SU
        $myArrayList.Remove("SU")                                  #SU is removed                                       
        $myArrayList.Clear()                                       # clear arraylist
      

    ## Operators in PowerShell

    Arithmetic Operators:

    1. + (Addition)
    2. - (Subtraction)
    3. * (Multiplication)
    4. / (Division)
    5. % (Modulus)
    6. ++ (Increment)
    7. - (Decrement)

    Comparison Operators:

    1. -**eq** (Equal)
    2. -**ne** (Not Equal)
    3. -**gt** (Greater Than)
    4. -**lt** (Less Than)
    5. -**ge** (Greater Than or Equal)
    6. -**le** (Less Than or Equal)
    7. **-like** (compares a test string)
    8. -match (pattern matching)

    Logical Operators:

    1. -**and** (Logical AND): Returns true if both conditions are true.
    2. -**or** (Logical OR): Returns true if at least one condition is true.
    3. -**not** (Logical NOT): Negates a condition, converting true to false and vice versa.
    4. -**xor** (Logical XOR): Returns true if one condition is true and the other is false.

    Special Operators:

    1. . (Dot Operator): Accesses properties and methods of objects.
    2. :: (Static Member Operator): Accesses static properties and methods of classes.
    3. | (Pipeline Operator): Passes objects from one command to another.
    4. ? (Where-Object Operator): Filters objects based on a condition.

    Assignment Operators: Perform operation and assign the result to variable

    1. == (Assignment)
    2. += (Addition Assignment)
    3. -**=** (Subtraction Assignment)
    4. *= (Multiplication Assignment)
    5. /= (Division Assignment)
    6. %= (Modulus Assignment)

    ## **Redirection, Split and Join Operators**

    > Redirection Operator

    **>>** Appending Redirection Operator

    **|** Pipeline Operator

    1
    2
    3
    
      Get-Process > System.txt           #redirect output of get-process to System.txt and create it if it doesn't exist
      Get-Services >> System.txt         #append the output of Get-Services to System.txt
      Get-ChildItem | Where-Object { $_.Extension -eq ".txt" } | Sort-Object Length #pass the output of one command as the input to another command.
    

    -join is used to concatenate an array of strings into a single string

    -**Split** method is used to split a string into an array of substrings based on a specified delimiter.

    ## **if Statement**

    1
    2
    3
    4
    
      if (condition) {
          # Code to execute when the condition is true
      }
      if (Get-LocalUser "Admin") {"I am Admin"}                #check if the current user is admin
    

    ### if …. else

    1
    2
    3
    4
    5
    6
    7
    
      $directoryPath = "C:\MyFolder"
      if (-not (Test-Path -Path $directoryPath -PathType Container)) {
          New-Item -Path $directoryPath -ItemType Directory
          Write-Host "Directory created: $directoryPath"
      } else {
          Write-Host "Directory already exists: $directoryPath"
      }
    

    ### if …. elseif …. else

    1
    2
    3
    4
    5
    6
    7
    8
    
      $userAccessLevel = "Guest"
      if ($userAccessLevel -eq "Admin") {
          Write-Host "Welcome, Admin! You have full access."
      } elseif ($userAccessLevel -eq "Jhon") {
          Write-Host "Welcome, Jhon! You have limited access."
      } else {
          Write-Host "Access denied. You are a Guest and have restricted access."
      }
    

    ## **Switch Statement**

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    
      Switch (<test-expression>)
      {
          <result1-to-be-matched> {<action>}
          <result2-to-be-matched> {<action>}
      }
        
      #Example
        
      $serviceName = "wuauserv"
        
      $serviceStatus = Get-Service -Name $serviceName
        
      switch ($serviceStatus.Status) {
          "Running" {
              Write-Host "$serviceName service is currently running. Stopping it..."
              Stop-Service -Name $serviceName -Force
              Write-Host "$serviceName service has been stopped."
          }
          "Stopped" {
              Write-Host "$serviceName service is currently stopped. Starting it..."
              Start-Service -Name $serviceName
              Write-Host "$serviceName service has been started."
          }
          default {
              Write-Host "$serviceName service is in an unexpected state: $($serviceStatus.Status)."
          }
      }
    

    ## **Loop Statements**

    ### **While**

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
      while (<condition>) {
          # Code to be executed while the condition is true
      }
        
      #Example
      $countdown = 10
      Write-Host "Starting Countdown:"
      while ($countdown -ge 0) {
          Write-Host "$countdown"
          Start-Sleep -Seconds 1  
          $countdown--
      }
      Write-Host "Countdown Complete!"
    

    ### For

    1
    2
    3
    4
    5
    
        
      for ($i = 1; $i -le 5; $i++) {
          $fileName = "file$i.txt"
          Write-Host "Creating $fileName..."
      }
    

    ### Foreach

    1
    2
    3
    4
    5
    6
    7
    8
    
        
      foreach ($item in $collection) {
          # Code to execute for each item
      }
        
      #loop on each file in directory and check if extension is png print filename
      foreach ($file in Get-ChildItem) {
      if ($file.Extension -eq ".png") {$file.name} }
    

    ## **Functions in PowerShell**

    • created by keyword **function**
    • param block is used to define input parameters
    • in PowerShell the return statement to specify the value that a function should return.
    • Static Parameters: are suitable for functions where the number and type of parameters are known in advance
    • Dynamic Parameters: are useful when you want the parameter names or data types to change depending on the situation. Dynamic parameters are defined using the DynamicParam
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
      function Add-Numbers {
          Param( [int]$a, [int]$b)
          $result = $a + $b
          Write-host $result
      }
      #if int is not spicified a & b will be treated as string  
        
      #CALL Function by two ways
      Add-Numbers -a 8 -b 1         
      Add-Numbers 8 1 
        
    
    1
    2
    3
    4
    5
    
      function Add-Numbers {
          Param( [int]$a = 5, [int]$b = 12 )
          $result = $a + $b 
          Write-host $result
      }
    
    • you can define parameters for your functions or cmdlets as mandatory, which means that the user must provide a value for these parameters when calling the function

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      
        function My-Function {
            param (
                **[Parameter(Mandatory=$true)]**
                [int] $MandatoryParameter , [Parameter ()] [int] $N
              
            )
              
         Write-Host "Mandatory parameter is $MandatoryParameter"
         Write-Host "NOT Mandatory parameter is $N " 
        }
        #If a mandatory parameter is not provided, PowerShell will prompt the user for the required input, and the command **won't execute** until the user provides the necessary values.
      
    • When you define Position parameters for a function or cmdlet, you can call that function or cmdlet and provide values for those parameters in a specific order, without needing to specify parameter names.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      
        function My-Function {
            param (
                [Parameter(Position=1)]
                [int] $Param1,
              
                [Parameter(Position=0)]
                [int] $Param2
            )
        #block of code  
        }
              
        #The first input you provide when calling My-Function will be assigned to $Param2 because it has a position of 0
        #the second input will be assigned to $Param1 because it has a position of 1.
      

    ## **Modules in PowerShell**

    1
    2
    3
    
      Install-Module -Name ModuleName                 #install module
      Import-Module ModuleName                        #import module in powershell
      Remove-Module ModuleName                        #remove module
    

    # Powershell ISE

    • built-in development environment and script editor for PowerShell on Windows operating systems.
    • It provided a graphical interface for writing, editing, and debugging PowerShell scripts and commands.
This post is licensed under CC BY 4.0 by the author.

Trending Tags