Windows PowerShell

Execution Policy

This command sets the PowerShell execution policy.

If you encounter an error such as …

{script path} cannot be loaded.
The file {script path} is not digitally signed.
You cannot run this script on the current system.
For more information about running scripts and setting execution policy,
see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.

You can set a Bypass execution policy for the current session.

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

Reference

This PowerShell script contains a menu to execute various tasks using functions.

UtilityMenuExample.ps1
function ListProcess
{
    Get-Process | Group-Object Company | Sort-Object Count -Descending
}

function ListEnvVars
{
    Get-ChildItem env:
}

function ListEventLog
{
    Get-EventLog -List
}

function Cleanup
{
    Write-Host "Delete files from $env:temp older than 24 hours"
    Get-ChildItem -path $env:temp | where {$_.Lastwritetime -lt (date).addhours(-24)} | remove-item
    <# Clear-RecycleBin #>
    $Shell = New-Object -ComObject Shell.Application
    $RecBin = $Shell.Namespace(0xA)
    $RecBin.Items() | %{Remove-Item $_.Path -Recurse -Confirm:$false}
}

function ShowMenu
{
        param (
            [string]$Title = 'Menu'
        )
        Write-Host "====== $env:USERPROFILE - $Title ======="

        Write-Host "1: List Running Processes"
        Write-Host "2: List Environment Variables"
        Write-Host "3: List Event Log"
        Write-Host "4: Clean Temp and Recycle Bin"
        Write-Host "q: quit"
}

do
{
        ShowMenu
        $input = Read-Host "Please make a selection"
        switch ($input)
        {
        '1' {
            Clear-Host
            ListProcess
        }
        '2' {
            Clear-Host
            ListEnvVars
        }
        '3' {
            Clear-Host
            ListEventLog
        }
        '4' {
            Clear-Host
            Cleanup
        }
        'q' {
            return
        }
        }
        pause
}
until ($input -eq 'q')

UtilityMenuExample.ps1

Node Version Manager for Windows

Download the nvm-setup.zip file from the windows-nvm repository.

Open the zip file, then open the nvm-setup.exe file.

Remove existing installations of Node.js or npm from Windows before installing NVM for Windows. This includes removing directories, e.g., C:\Program Files\nodejs. NVM’s symlink will not overwrite an existing or empty installation directory. For more info, how-to-completely-remove-node-js-from-windows.

The Setup NVM for Windows installation wizard will walk you through the setup steps, including choosing the directory where both NVM for Windows and Node.js will be installed.

NVM for Windows requires administrative rights, use Sudo for Windows as needed.

Open WSL

Use wsl.exe or bash.exe to open WSL in the current working directory.

Additionally, PowerShell will execute wsl without the extension. e.g.,

# open active WSL linux bash in CWD

wsl

Setting System Environment Variables

This PowerShell script accepts parameters for setting system environment variables for Java development in Windows.

java.ps1

Sudo for Windows

It can be annoying as hell when you get a PermissionDenied error and have to open another shell with Run as administrator to execute the command. Wouldn’t it be nice to just use sudo like we can with Unix. Thanks to Luke Sampson’s sudo.ps1, we can use sudo in PowerShell. This install uses the Scoop command-line installer for Windows.

# install Scoop
iex (new-object net.webclient).downloadstring('https://get.scoop.sh')

set-executionpolicy unrestricted -s cu -f

scoop install sudo

Reference

Using Git with ssh-agent

posh-git and Git for Windows should include everything you need to setup an ssh-agent. Once you have the module installed, you can start the agent as follows:

Import-Module ~\Documents\WindowsPowerShell\Modules\posh-git\posh-git
Set-Alias ssh-agent "$env:ProgramFiles\git\usr\bin\ssh-agent.exe"
Set-Alias ssh-add "$env:ProgramFiles\git\usr\bin\ssh-add.exe"
Start-SshAgent -Quiet

Resources

Git, Readline Bash and Custom Prompt

PowerShell modules can enhance functionality and the user interface. In this example, the profile is updated to use git over an SSH connection, readline settings for a bash like experience, custom color output and a custom prompt.

Commands

PowerShell Commands