Windows Powershell Potpourri


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
Pay attention to the InterfaceAlias
This will be used to Set DNS with Set-DNSClientServerAddress
Set DNS quickly through Powershell
Set-DNSClientServerAddress "Ethernet" –ServerAddresses ("","")
Clear Client DNS Cache
Update Office Click to run
"C:\Program Files\Common Files\microsoft shared\ClickToRun\OfficeC2RClient.exe" /update user displaylevel=false forceappshutdown=true

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 $ #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