rss logo

My PowerShell Notes

Here lies my PowerShell notes.

E-mail

  • A function to send emails :
Function Mail
 {
        param ([string]$sujet)

        $encoding = [System.Text.Encoding]::UTF8
        Send-MailMessage -To destination@shebangthedolphins.net -Subject $sujet -From source@shebangthedolphins.net -smtpserver smtp.shebangthedolphins.net -Body $sujet -Encoding $encoding
 }

Mail "Subject"

Files

  • Redirect output to file :
Get-WmiObject -Class Win32_service | Select-Object Name,DisplayName,startmode | Out-File -FilePath C:\services.txt
  • Remove a character from files name, example here where we remove o character for all mp4 files :
Get-ChildItem -filter *mp4 | Rename-Item -NewName {$_.Name -replace "o"}
  • Compare content of two csv files :
Compare-Object (Get-Content .\kb01.csv | Sort-Object | Sort-Object -unique) (Get-Content .\kb02.csv | Sort-Object | Sort-Object -unique)
  • Compare two files with hash :
 $(Get-FileHash C:\01.zip | Select-Object -Property Hash).Hash -eq $(Get-FileHash C:\02.zip | Select-Object -Property Hash).Hash

Miscellaneous

  • Run unsigned script :
PowerShell.exe -ExecutionPolicy Bypass -File Script.ps1
  • Set time :
Set-Date -date "12/7/2017 4:00 PM"
  • Show date with prepredefined format :
Get-Date -Format "dd/MM/yyyy"
  • Export KB list to csv :
Get-HotFix | Select-Object hotfixid | Sort-Object hotfixid  | Export-Csv -path c:\$(get-date -Format "yyyy.MM.dd")_kb.csv -NoTypeInformation
  • Know if a KB is installed :
Get-HotFix | Select-Object hotfixid | Sort-Object hotfixid  | Where-Object { $_ -match ".*KB156.*" }
  • Modify Last Write Time :
$a = Get-Item '.\Download\myfile.txt'
$a.LastWriteTime = "10/03/2016 8:00 AM"
  • Extract multiple Dell drivers exe :
foreach ($app in $(Get-ChildItem | ? { $_.Name -match "exe" })) { Start-Process -FilePath ".\$($app.Name)" -ArgumentList "/s /e=.\_$($app.Name)" -Wait }

Arrays

  • Declare an array :
$myUsers = @("Stan Marsh","Kyle Broflovski","Eric Cartman","Kenny McCormick")
    • Or :
$myUsers = New-Object System.Collections.ArrayList
[void]$myUsers.AddRange( ("Stan Marsh","Kyle Broflovski","Eric Cartman","Kenny McCormick") )

Network

  • Get ip address for all Interfaces :
Get-NetIPAddress
  • Set ip address to Ethernet Interface :
New-NetIPAddress -InterfaceAlias "Ethernet" -ipaddress "192.168.1.111" -PrefixLength 24 -DefaultGateway "192.168.1.254"
  • Remove ip address to Ethernet Interface :
Remove-NetIPAddress -IPAddress 192.168.1.111 -Confirm:$false
  • Change MAC address of connected network devices :
Get-NetAdapter | Where-Object { $_.status -eq 'up' } | set-netadapter -MacAddress '001122334455' -Confirm:$flase
  • Modify network connection profile :
Get-NetConnectionProfile
Set-NetConnectionProfile -InterfaceIndex 10 -NetworkCategory Private

Programs

  • Get programs list :
Get-WmiObject Win32_product | Select-Object -Property Name,Vendor,Version | Sort-Object -Property name
  • Export programs list to csv file :
Get-WmiObject Win32_product | Select-Object -Property Name,Vendor,Version | Sort-Object -Property name | Export-Csv -Path C:\prog_list.csv

Services and Process

Services

  • List Services :
Get-WmiObject -Class Win32_Service | Select-Object Name,DisplayName,StartMode
  • List Services
Get-Service | Select-Object Name,DisplayName,StartType

Process

  • Get process with users names :
Get-Process -IncludeUserName | Select-Object UserName,SessionId
  • List logged on users :
Get-Process -IncludeUserName | Select-Object Username,processname | Where-Object { $_.ProcessName -eq "explorer" }  | ForEach-Object {"{1}" -f $_.UserName.ToString().Split('\') }
Get-Process -IncludeUserName | Select-Object Username,processname | Where-Object { $_.ProcessName -eq "explorer" } | %{"{0}\{1}" -f $_.UserName.ToString().Split('\')[0],$_.UserName.ToString().Split('\')[1]}
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address