PowerShell: Listing Windows Updates

Posted Leave a commentPosted in PowerShell

There is a not widely known COM object that you can use to list all the installed Windows Updates on a machine. Here is the code: $Session = New-Object -ComObject Microsoft.Update.Session $Searcher = $Session.CreateUpdateSearcher() $HistoryCount = $Searcher.GetTotalHistoryCount() $Searcher.QueryHistory(1,$HistoryCount) | Select-Object Date, Title, Description

PowerShell: Displaying Balloon Tip

Posted Leave a commentPosted in PowerShell

Let’s assume your script wants to share status information via a balloon message in the system tray area. Here is a sample: [system.Reflection.Assembly]::LoadWithPartialName(‘System.Windows.Forms’) | Out-Null $balloon = New-Object System.Windows.Forms.NotifyIcon $path = Get-Process -id $pid | Select-Object -ExpandProperty Path $icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) $balloon.Icon = $icon $balloon.BalloonTipIcon = ‘Info’ $balloon.BalloonTipText = ‘Completed Operation’ $balloon.BalloonTipTitle = ‘Done’ $balloon.Visible […]

PowerShell: Extracting Icons

Posted Leave a commentPosted in PowerShell

To extract an icon from a file, use .NET Framework methods. Here is a sample that extracts all icons from all exe files in your Windows folder (or one of its subfolders) and puts them into a separate folder: [System.Reflection.Assembly]::LoadWithPartialName(‘System.Drawing’) | Out-Null $folder = “$env:temp\icons” md $folder -ea 0 | Out-Null dir $env:windir *.exe -ea […]

PowerShell: Clean Your Temp Folder

Posted Leave a commentPosted in PowerShell

When disk space gets low, you may want to clean up your temporary folder. The code deletes all files that are older than 30 days to make sure you’re not dumping anything that’s still needed: $cutoff = (Get-Date) – (New-TimeSpan -Days 30) $before = (Get-ChildItem $env:temp | Measure-Object Length -Sum).Sum Get-ChildItem $env:temp | Where-Object { […]

PowerShell: Appending CSV Data

Posted 1 CommentPosted in PowerShell

To append a CSV file with new data, first of all make sure the type of data you append is the same type of data already in a file (or else column names will not match). This creates a list of unique running processes (so it will not list duplicate process names): $filepath = “$env:temp\processes.csv” […]

PowerShell: Finding Files Only or Folders Only

Posted Leave a commentPosted in PowerShell

In PowerShell v2, to list only files or only folders you had to do filtering yourself: Get-ChildItem $env:windir | Where-Object { $_.PSIsContainer -eq $true } Get-ChildItem $env:windir | Where-Object { $_.PSIsContainer -eq $false } In PowerShell v3, Get-ChildItem is smart enough to do that for you: Get-ChildItem $env:windir  -File Get-ChildItem $env:windir –Directory

PowerShell: Restarting Computers

Posted Leave a commentPosted in PowerShell

In PowerShell v3, Restart-Computer now has a number of useful new parameters. For example, you can restart a remote machine and wait for the reboot process to finish. -Wait: Halts the script until the machine has rebooted -Timeout: Seconds to wait for the machine to restart -For: Considers the computer to have restarted when the […]

PowerShell: Colorizing PowerShell ISE v3

Posted Leave a commentPosted in PowerShell

The new PowerShell ISE script editor in PowerShell v3 lets you customize a lot of colors, so if a particular color does not show well on a projector, for example, simply change it. You can do that via GUI, but you can also do it programmatically. For example, from your profile script: $psISE.Options.RestoreDefaultConsoleTokenColors $psISE.Options.RestoreDefaultTokenColors $psISE.Options.RestoreDefaultXmlTokenColors […]

PowerShell: Getting Relative Dates

Posted Leave a commentPosted in PowerShell

Here’s a quick and fast way of generating relative dates in any format: (Get-Date).AddDays(-1).ToString(‘yyyy-MM-dd’) This would return yesterday’s date in the format year-month-day. Remove the ToString() method if you want the relative date as a true DateTime object. This returns all error events from the System event log in the past 48 hours: Get-EventLog -LogName […]