My PowerShell Notes

Here are some PowerShell notes.

Notes

Execute unsigned script

PowerShell.exe -ExecutionPolicy Bypass -File Script.ps1

Delete 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"}

Change network connection profile

Get-NetConnectionProfile
Set-NetConnectionProfile -InterfaceIndex 10 -NetworkCategory Private

Set time

Set-Date -date "12/7/2017 4:00 PM"

Export KB list to csv

Get-HotFix | Select-Object hotfixid | Sort-Object hotfixid  | Export-Csv -path c:\kb.csv -NoTypeInformation

Know if a KB is installed

Get-HotFix | Select-Object hotfixid | Sort-Object hotfixid  | Where-Object { $_ -match ".*KB156.*" }

Compare two csv files

Compare-Object (Get-Content .\kb01.csv | Sort-Object | Sort-Object -unique) (Get-Content .\kb02.csv | Sort-Object | Sort-Object -unique)

Change Last Write Time

$a = Get-Item '.\Download\myfile.txt'
$a.LastWriteTime = "10/03/2016 8:00 AM"

Extract multiple Dell drivers

foreach ($app in $(Get-ChildItem | ? { $_.Name -match "exe" })) { Start-Process -FilePath ".\$($app.Name)" -ArgumentList "/s /e=.\_$($app.Name)" -Wait }

Services

List Services

Get-WmiObject -Class Win32_Service | Select-Object Name,DisplayName,StartMode
Get-Service | Select-Object Name,DisplayName,StartType

Export to file

Get-WmiObject -Class Win32_service | Select-Object Name,DisplayName,startmode | Out-File -FilePath C:\services.txt

E-mail

A function to send email

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"

Network

Get ip address from all Interface

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
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :