Florian Winkelbauer

Learning New Old Tricks

Sometimes, you discover something, that others have known for a long time. Last week, I’ve discovered how useful a ~/bin directory can be.

Linux

After creating a ~/bin directory on my Ubuntu machine, the directory is already part of the PATH variable. Any executable or script put in the ~/bin directory can be turned into a global available command after changing permissions and reloading the profile:

mkdir ~/bin
cp /path/to/script ~/bin
chmod +x ~/bin/script
. ~/.profile

Windows

I had to manually add ~/bin to the PATH variable:

$homeBinDir = Join-Path $env:USERPROFILE 'bin'
New-Item $homeBinDir -ItemType Directory -Force | Out-Null
[Environment]::SetEnvironmentVariable('Path', [Environment]::GetEnvironmentVariable('Path', 'User') + ';' + $homeBinDir, 'User')

Windows uses two different shells: cmd.exe and powershell.exe. You can use .bat, .cmd and .ps1 scripts in PowerShell, but you cannot use .ps1 in Cmd. Because of this, I’ve written a simple “wrapper script” which I can use to make a .ps1 script available in Cmd:

@ECHO OFF
PowerShell.exe -NoProfile -File "%~dpn0.ps1"

This .cmd script will call a .ps1 with the same name (e.g. foo.cmd will call foo.ps1). Such a script can also be used to start a .ps1 script as an Administrator in a new process:

@ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"

Running foo in:

Usage Examples

So we’ve got our bin directory set up, now what? Well, here’s a short list of scripts which I’m using on my machines: