Skip to content

Instantly share code, notes, and snippets.

@bklockwood
bklockwood / get-installedsoftware.ps1
Created August 3, 2016 12:34
List installed software
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | `
Where-Object {$_.Displayname -ne $null } | `
Select-Object Publisher, DisplayName, DisplayVersion | `
Sort-Object Publisher | `
Format-Table -AutoSize `
@bklockwood
bklockwood / meminfo.ps1
Last active June 17, 2016 10:18
Memory diagnosis
#this works for win10; older windows may not have same-same wmi class members
$physmem = (Get-CimInstance Win32_PhysicalMemory | select BankLabel, DeviceLocator, Capacity, ConfiguredClockSpeed, Manufacturer, PartNumber | Format-Table -AutoSize)
$pfsettings = (Get-CimInstance Win32_PageFileSetting | Select-Object Name, InitialSize, MaximumSize | Format-Table -AutoSize)
$pfuse = (Get-CimInstance Win32_PageFileUsage | select Name, Status, CurrentUsage, PeakUsage | Format-Table -AutoSize)
$pfdiskuse = (Get-CimInstance Win32_PageFile | Select-Object Name, Status, Compressed, FileSize | Format-Table -AutoSize)
$diskdrives = (get-psdrive | ?{$_.Provider.Name -eq "FileSystem"} | Format-Table -AutoSize)
$autopf = (Get-CimInstance Win32_ComputerSystem).AutomaticManagedPagefile
if ($autopf -eq $true) {
Write-Output "Pagefile settings ARE DEFAULT (automatically managed)."
} else {

Brief overview of how Windows Update (WU) works, as I understand it:

  1. WU downloads a file called wsusscn2.cab from the MS content delivery network point nearest you. Depending on various network factors, this is usually completed within 10-60 seconds. (I have written a short script to manually verify this)
  2. The cab file is cryptographically signed. This signature is checked, and the cab is extracted. The cab contains more cabfiles, also crypto-signed. Their signatures are checked and they are extracted.
  3. These files are parsed against the live windows system to see which of the available updates actually need to be applied to this PC. WU tries to get is many patches into one 'install-and-reboot' operation as it can, but sometimes successive reboots will be needed.
  4. After the processing is complete, a list of eligible updates is displayed, and the user is asked to choose which ones to install.
  5. After user makes his/her choices and click the
@bklockwood
bklockwood / print-webpage.ps1
Created April 7, 2016 04:20
Print a webpage
$IE = new-object -ComObject "InternetExplorer.Application"
$webpage = "http://bestforpuzzles.com/daily-quick-crossword/puzzles/2016-04/dq1-2016-04-06.html"
$IE.Navigate($webpage)
$IE.ExecWB(6,2)
@bklockwood
bklockwood / versioning-commands.ps1
Last active March 5, 2016 01:13
Version numbers in Win10 via PS
When WINVER says "Version 1511 (OS Build 10586.104)" ...
PS> (Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').ProductName
Windows 10 Pro
PS> (Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').ReleaseID
1511
PS> (Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').CurrentVersion
6.3
@bklockwood
bklockwood / gist:5a881b79f2c0947283da
Last active January 27, 2016 17:59
Slow Win7 WU
From a fresh installed Windows 7 system, WU can take hours to deleiver the first list of updates. Some observations:
* If I use offlinewucheck (https://gist.github.com/bklockwood/716d9f582dd78a9e6e9e) on a VM, the download part (wsusscn2.cab) takes less
than a minute; local processing takes ~30 minutes.
* Others report hours. One person reported 23 hours, on an i7 with SSD and 12GB of RAM!
* Unpacking wsusscn2.cab requires a fair amount of crypto (sig checks) and produces THOUSANDS of files.
* I have hazy memory of it overwhelming 2GB systems. Never completes at all; produces 'low virtual memory' errors.
* A system with most updates applied completes the task quickly.
Experiments to run:
function Get-UpdateList {
<#
.SYNOPSIS
Gets an ISearchResult containing updates from Windows Update.
.PARAMETER Computername
The target computer.
Cannot use an array of computernames here.
Defaults to the local PC.
function test-remoteicm {
[CmdletBinding()]
Param ()
$true
write-output "hello"
}
@bklockwood
bklockwood / cim.ps1
Created December 18, 2015 03:19
Find CIM methods and parameters in powershell
PS C:\> $wumethods = gcls -Namespace ROOT/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession
PS C:\> $wumethods.CimClassMethods
Name ReturnType Parameters Qualifiers
---- ---------- ---------- ----------
SetCallerID UInt32 {CallerID} {implemented}
ApplyApplicableUpdates UInt32 {HResult} {implemented}
ScanForUpdates UInt32 {OnlineScan, SearchCriteria, HResult, Updates} {implemented}
ScanForUpdatesWithProxy UInt32 {BypassList, BypassLocal, ProxyAddress, SearchCriteria...} {implemented}
DownloadUpdates UInt32 {Updates, HResult} {implemented}
@bklockwood
bklockwood / Setup-PC.ps1
Last active April 22, 2021 14:35
Setting up a new PC
#Activate Windows
slmgr.vbs /ato
#Enable RDP (don't require Network Level Auth)
set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -Value 0
set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -name "UserAuthentication" -Value 0
Enable-NetFirewallRule -Name RemoteDesktop-UserMode-In-TCP
#Allow ping, v4 and v6
New-NetFirewallRule -Name Allow_Ping4 -DisplayName "Allow IPV4 Ping" -Protocol ICMPv4 -Enabled True -Profile Any -Action Allow