Windows Powershell Potpourri

Purpose:

The purpose of this article is to display a set of commonly used Windows Powershell Scripts

To recursively remove a folder that may contain too many folders for Windows to handle
remove-item -path "C:\Program Files\ScienceLogic" -recurse -verbose
Get System Boot Time
systeminfo | find /i "Boot Time"
Get all files in a folder and subfolders
get-childitem -file -path "c:\Temp" -Recurse -Name | Split-path -Leaf
Remove all items in Downloads folders for all users with local profile
ls c:\Users\*\Downloads -Recurse -Directory | REMOVE-ITEM -Recurse
Remove all Items that start with Archive in current Directory
Get-ChildItem | Where-Object Name -Like 'Archive*' | ForEach-Object { Remove-Item -LiteralPath $_.Name}
Import from a CSV with Name header Write Name
Import-csv .\Computers.csv | ForEach-Object { Write-host "$($_.Name)"}
Get Client DNS from Powershell
get-dnsclientserveraddress
Pay attention to the InterfaceAlias
This will be used to Set DNS with Set-DNSClientServerAddress
Set DNS quickly through Powershell
Set-DNSClientServerAddress "Ethernet" –ServerAddresses ("192.168.22.11","192.168.22.13")
Clear Client DNS Cache
Clear-DnsClientCache
Update Office Click to run
"C:\Program Files\Common Files\microsoft shared\ClickToRun\OfficeC2RClient.exe" /update user displaylevel=false forceappshutdown=true
Check Windows Update History
wmic qfe list

Check for Hidden Windows Updates

Create new Microsoft.Update.Session
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
Create new Update Searcher
$UpdateSearcher = $UpdateSession.CreateupdateSearcher()
Search for Updates
$Updates = @($UpdateSearcher.Search("IsHidden=0 and IsInstalled=0").Updates)
Out put Title and Updates
$Updates | Select-Object Title

Install a specific Update

Install Module PSWindowsUpdate
Install-Module PSWindowsUpdate
Check for updates alternative
Get-WindowsUpdate
Install all available updates
Install-WindowsUpdate
Install a specific KB
Get-WindowsUpdate -Install -KBArticleID 'KB5021233'
If you get an error about Nuget
[Net.servicepointmanager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

Hash password to Registry

#Prompts for a password and saves to Registry HKCU:\Software\Nunyabusiness EncPwd String
$pass = Read-Host "Enter Password"
$pass = $pass | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
# Set variables to indicate value and key to set
$RegistryPath = 'HKCU:\Software\Nunyabusiness'
$Name = 'EncPwd'
$Value = $pass
# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
New-Item -Path $RegistryPath -Force | Out-Null
}
# Now set the value
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType String -Force

Example for loops

$FolderPath = "C:\temp\"
$folders = Get-ChildItem -Path $TSRedirectPath | Where-Object {$_.PsIsContainer}
#Example foreach loop
foreach ($folder in $folders) {
    #Write each folder
    Write-Host $folder.FullName #Name and Path of Folder
    Write-Host $folder.name #Name of Folder
}
#Example for loop
for (($i = 0); $i -lt $folders.Count; $i++)
{
    $folders[$i].Fullname #Name and Path of Folder
    $folders[$i].name #Name of Folder
}