Skip to content

Latest commit

 

History

History
202 lines (156 loc) · 6.22 KB

README.md

File metadata and controls

202 lines (156 loc) · 6.22 KB

Sharp Ninja's Powershell Snippets

A collection of PowerShell tools you can add to your profile.

docs 🏗️

Windows

  1. Clone this repository to $env:OneDrive/Documents/Snippets
  2. In an Administrator elevated editor, edit $PROFILE.AllUsersAllHosts.
  3. Add $env:Snippets="$env:OneDrive/Documents/PowerShell/Snippets" to the end and save it.

Linux, WSL, MacOS

  1. Clone this repository to /opt/microsoft/powershell/7/Snippets
  2. In an Administrator elevated editor, edit $PROFILE.AllUsersAllHosts.
  3. Add $env:Snippets="/opt/microsoft/powershell/7/Snippets" to the end and save it.

OR

Execute this script...

curl 'https://raw.githubusercontent.com/PS-Services/Snippets/master/linux-setup.sh' -v | /bin/bash
Win *nix Script Description
bing.ps1 Search Bing from Powershell.
clean‑folder.ps1 Remove all bin and obj folders in current path.
github.ps1 Set $env:GITHUB first to the root of your github repositories. Use hub or hub <repository> to go to those folders.
oh‑my‑posh.ps1 Initializes Oh-My-Posh for the current PowerShell
_repos.ps1 A unified repository query system.
chocolatey.ps1 Setup Chocolatey profile in PowerShell.
devmode.ps1 Startup VS 2022 Dev Mode Tools.

Place calls to these files in your $PROFILE

All scripts work in both PowerShell Core and Windows PowerShell 5.1!

Example Windows $PROFILE

$env:Snippets="$env:OneDrive\Documents\PowerShell\Snippets"

if ($env:VerboseStartup -eq 'true') {
    [switch]$Verbose = $true
}
else {
    [switch]$Verbose = $false
}

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process

try {
    Import-Module Microsoft.PowerShell.Utility

    if (Test-Path $env:Snippets) {
        Push-Location
        Set-Location $env:Snippets
        $snippets = Get-ChildItem *.ps1
        Pop-Location

        $snippets.FullName | ForEach-Object -Process {
            $snippet = $_

            . $snippet -Verbose:$Verbose
        }
    }
    else {
        Write-Verbose "No directory found at [$env:Snippets]" -Verbose:$Verbose~
    }

    Write-Verbose 'PowerShell Ready.' -Verbose:$Verbose
}
catch {
    Write-Host $Error
}
finally {
    Write-Verbose "Leaving $Profile" -Verbose:$Verbose
}

Example Linux, Wsl, MacOS $PROFILE

# $env:VerboseStartup = 'true'
$profileScript = Split-Path $PROFILE -Leaf

if((-not $env:Snippets) -or (-not (Test-Path $env:Snippets))) {
    $env:Snippets = "$env:HOME/.config/powershell"
}

if ($env:VerboseStartup -eq 'true') {
    [switch]$MasterVerbose = $true
}
else {
    [switch]$MasterVerbose = $false
}

try {
    Push-Location -Verbose:$MasterVerbose

    Import-Module Microsoft.PowerShell.Utility #-Verbose:$MasterVerbose

    $env:Snippets = Join-Path $env:Snippets -Child Snippets -Verbose:$MasterVerbose

    if (-not (Test-Path $env:Snippets -Verbose:$MasterVerbose)) {
        git clone "https://github.com/sharpninja/Snippets.git"
    } else {
        Write-Verbose "[$profileScript] Found $env:Snippets" -Verbose:$MasterVerbose
    }

    if (Test-Path $env:Snippets -Verbose:$MasterVerbose) {
        Push-Location -Verbose:$MasterVerbose
        Set-Location $env:Snippets -Verbose:$MasterVerbose
        $snippets = Get-ChildItem *.ps1 -Verbose:$MasterVerbose -Exclude _common.ps1
        Pop-Location -Verbose:$MasterVerbose

        $resultList = @()
        $snippets.FullName | ForEach-Object -Verbose:$MasterVerbose -Process {
            try {
                $snippet = $_
                $snippetName = Split-Path $snippet -Leaf
                Write-Verbose "[$profileScript]->[$snippetName] Calling with: -Verbose:`$$MasterVerbose" -Verbose:$MasterVerbose
                $result = $null
                $result = . $snippet -Verbose:$MasterVerbose
            }
            catch {
                Write-Error "[$profileScript]->[$snippetName] Error: $_"
            }
            finally {
                $report = "[$snippetName]->[ $result ]"
                $resultList += $report;
            }
        }

        if ($resultList.Length -gt 0) {
            "[$profileScript] Snippet Results`n---`n$([System.String]::Join("`n", $resultList))`n---`n"
        }
        else {
            "[$profileScript] No snippets where executed."
        }
    }
    else {
        Write-Verbose "[$profileScript] No directory found at [$env:Snippets]" -Verbose:$MasterVerbose
    }
}
catch {
    Write-Error "[$profileScript] $_"
}
finally {
    Pop-Location
    Write-Verbose "Leaving $Profile" -Verbose:$MasterVerbose
}

Get-Alias -Verbose:$MasterVerbose `
    | Where-Object -Property Description -imatch 'snippet' -Verbose:$MasterVerbose `
    | Format-Table Name, Description -AutoSize -Verbose:$MasterVerbose

Write-Verbose 'PowerShell Ready.' -Verbose:$MasterVerbose

Repositories

Common

  • dn dotnet
  • dt dotnet tool
  • ng nuget
  • np NPM
  • pp pip
  • pps pip-search

Windows

  • repos Search all OS repos
  • wq winget
  • scp scoop
  • ch chocolatey

EXAMPLE

repos oh-my-pos

Repo       Command
----       -------
scoop      install [email protected]
sudo choco install oh-my-posh --version 18.5.0 -y
winget     install XP8K0HKJFRXGCK -s msstore # oh-my-posh

Linux

  • repos Search all OS repos
  • ap apt
  • zy zypper
  • sn snap
  • br homebrew