Powershell Few-Liners (Miscellaneous)

  • Post comments:0 Comments

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

 

Leave a Reply