Skip to content

Latest commit

 

History

History
118 lines (86 loc) · 3.29 KB

PowerShell.md

File metadata and controls

118 lines (86 loc) · 3.29 KB

PowerShell Cheatsheet

Index

Global regex search and replace

Search with directory exclude

Searches all JavaScript files for classes that begin with Test, excluding node_modules dir:

gci -Recurse -Include *.js | ?{ $_.FullName -inotmatch 'node_modules' } | Select-String '\bclass\s+Test'

Search and replace

Replaces any licenses within package.json files with MIT (original files will be overwritten):

gci -Recurse -Filter package.json | Select-String '"license": ".+?"' -List | %{
    ((gc $_.Path -Raw -Encoding utf8) -replace '"license": ".+?"', '"license": "MIT"') | Out-File $_.Path -NoNewline -Encoding utf8 }

Another example of traversing:

gci -Recurse -Include *.py | %{
    ((gc $_.FullName -Raw -Encoding utf8) -replace 'assertNotEqual', 'assert_not_equals') | Out-File $_.FullName -NoNewline -Encoding utf8 }

Remarks:

  • The -List option will group multiple matches within one file into single result.
  • The -Raw option will read entire file contents as one string.
  • The -NoNewline option will not add new line symbols to the end of the file.

Recursively remove specified directories

Recursively scans and removes specified directories (build and node_modules):

gci -Recurse -Include build, node_modules -Directory | rmdir -Recurse -Force

File IO

Extract ZIP archive to specified folder

Expand-Archive "$env:USERPROFILE\Downloads\ninja-win.zip" "$env:LOCALAPPDATA\Programs\ninja"

Illform-encoded file fix

UTF16LE illformed file fix (removes BOM, zero bytes and CR-only line breaks):

$CR = [byte][char]"`r"
$NL = [byte][char]"`n"
$bytes = [io.file]::ReadAllBytes('resource.h')
$bytes_fixed = [collections.generic.list[byte]]::new()
for ($i = 2; $i -lt $bytes_in.Length; $i++) {
    $byte = $bytes[$i]
    if ($byte -ne 0) {
	if (($byte -eq $CR) -and ($bytes[$i + 1] -ne $NL)) {
            continue
	}
        $bytes_fixed.Add($byte)
    }
}
[io.file]::WriteAllBytes('resource_fixed.h', $bytes_fixed)

VS Code

Open settings file

Open VS Code, press Ctrl+Shift+P, type Open Settings (JSON).

Disable Integrated Console on Startup

{
    "powershell.integratedConsole.showOnStartup": false
}

Unsing custom PowerShell version as default

{
    "terminal.integrated.shell.windows": "C:/Program Files/PowerShell/7/pwsh.exe"
}

Disabling code lens for PowerShell

{
    "[powershell]": {
        "editor.codeLens": false
    }
}