Powershell Few-Liners (Miscellaneous)

This article is part of the few-liner series, where you can find some useful powershell examples. Of course, you need to adjust the values/paths to your needs. Also, I know there may be other ways to achieve the same results but this is not a base for an “which-command-is-better” competition. Depending on the time you read this post, there may be new commandlets making things easier but I do not regulary update this blog series.

 

Serialize / de-serialize files

$object = Get-Item "C:\Windows\Notepad.exe"
$serialized = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes([Management.Automation.PSSerializer]::Serialize($object)))
$deserialized = [Management.Automation.PSSerializer]::Deserialize([Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($serialized)))

 

Kill all running processes within a certain path

Get-Process | ? { $_.Path -like "*Citrix*" } | % { Taskkill /F /IM ([System.IO.Path]::GetFilename($_.Path)) }

– or –

Get-Process | ? { $_.Path -like "*Citrix*" } | Stop-Process -Force

 

Get a list of special folder names and resolve their path

[Enum]::GetNames([Environment+SpecialFolder])
[System.Environment]::GetFolderPath("MyVideo")

 

Disable the wireless adapter (works in most cases)

Get-NetAdapter | ? {$_.InterfaceDescription -like "*wireless*"} | Disable-NetAdapter

 

Search for string in multiple files and print file names containing the string

Get-Childitem -Path .\ -Recurse -Include *.txt,*.log,*.xml | Select-String "stringToSearch" -List | Select Path
Get-Childitem -Path .\ -Recurse -Include *.* | Select-String "stringToSearch" | Add-Content .\Out.txt
Get-ChildItem C:\Daten\Intune*.log | Select-String "Launch Win32AppInstaller" | select path,linenumber,line | fl

 

List all Hotfixes

gwmi -Class Win32_ReliabilityRecords -filter "Sourcename='Microsoft-Windows-WindowsUpdateClient'" | Select Message,ProductName,@{ Label="KB"; Expression={([regex]::Matches($_.ProductName, "KB\d{7}")).Value}},@{ Label="Date"; Expression={[System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeGenerated)}} | sort -Property Date -Descending | fl
gwmi -Class Win32_ReliabilityRecords -filter "Sourcename='Microsoft-Windows-WindowsUpdateClient'" | Select Message, ProductName, @{ Label="KB"; Expression={([regex]::Matches($_.ProductName, "KB\d{7}")).Value}}, @{ Label="Date"; Expression={[System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeGenerated)}} | ? { ![string]::IsNullOrEmpty($_.KB) } | sort -Property Date -Descending | fl

 


Posted

in

by

Tags:

Comments

Leave a Reply