Skip to content

Instantly share code, notes, and snippets.

@chrisdpa-tvx
Last active October 10, 2018 15:45
Show Gist options
  • Save chrisdpa-tvx/f22186c38cf8281c9f8ec41733edd050 to your computer and use it in GitHub Desktop.
Save chrisdpa-tvx/f22186c38cf8281c9f8ec41733edd050 to your computer and use it in GitHub Desktop.
Cross over commands for those forced to use powershell

Recursive Grep

unix:

grep -R 'bruce'

Powershell

Get-ChildItem D:\path -recurse | Select-String -pattern 'bruce' | group path | select name
$ Get-ChildItem -Path d:/path -Filter 'installation.log' -Recurse | select-string 'successful' -list

Read range of characters from a file

unix:

todo

Powershell

Get-Content filename -Encoding Byte | Select-Object -Index (1000..2000) | FOREACH {WRITE-HOST –object ([CHAR]$_) –nonewline }

Convert from Base64 to Text

unix:

echo 'YnJ1Y2UK' | base64
bruce

Powershell

[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String('YnJ1Y2UK'))
bruce

List all commands available

Powershell

Get-Module -ListAvailable | `
foreach {"`r`nmodule name: $_"; "`r`n";gcm -Module $_.name -CommandType cmdlet, function | select name}

Resize partition to maximum size

Powershell

Get-Partition -DriveLetter G | %{Update-disk  $_.DiskNumber}
Resize-Partition -DriveLetter G -Size $(Get-PartitionSupportedSize -DriveLetter G).SizeMax

Base64 Encoding

Powershell

[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes([System.IO.File]::ReadAllText('text')))
YnJ1Y2UNCg==

[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String('YnJ1Y2UNCg=='))
bruce

Resize a terminal screen (non-interactive)

Powershell

$host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(180,500)

Update values in an XML document

$node = configuration.etl.dbDateFormat
$key = "format"
$value = 'yyyy-MM-dd'
$conf_path = "path/to/file.xml";
$conf = [xml](Get-Content $conf_path);
$entry = $conf.$node;
if($entry) {
  $entry.SetAttribute("$key", "$value");
} else {
  $entry = $conf.CreateNode("$node");
  $entry.SetAttribute("Key", "$key");
  $entry.SetAttribute("Value", "$value");
  $conf.Configuration_list.AppendChild($entry);
}
$conf.Save($conf_path);

Start a script in the background

Start-process powershell -windowstyle Hidden -Verb runAs "$cmd"

Create a Local User

And add it to Administrators and Remote desktop users

new-localuser UserName -password $(convertto-securestring xxxxxxxxx -AsPlainText -Force)
add-localgroupmember -group 'Administrators' -Member UserName
add-localgroupmember -group 'Remote Desktop Users' -Member UserName

Get recent tomcat errors

gci 'C:\Program Files\Apache Software Foundation\tomcat\' -filter 'tomcat8-stderr.*' -recurse | gc -tail 100

Show Physical Memory Allocated to a Program

get-process AISSrv | Group-Object -Property ProcessName,Id  | `
Format-Table Name, @{n='Phys Mem Alloc (MB)';e={'{0:N0}' -f (($_.Group|Measure-Object WorkingSet64 -Sum).Sum / 1KB)};a='right'} -AutoSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment