1 minute read

Sure am very late to the show, but I recently started to use PowerShell instead of the good old cmd.exe, which honestly, sucks.

image-center

There’s a few tricks for PowerShell to feel “useable” so while I’m not going to teach how to write scripts for it, it’s nice to be able to look at my own posts when I have forgotten how I setup the thing.

First of all make sure your profile directory exists and add also a scripts directory:

> mkdir $HOME\Documents\WindowsPowerShell
> mkdir $HOME\Documents\Scripts

Second, edit your profile (it’s loaded at PowerShell startup):

> notepad $PROFILE

Add something like that inside and save:

New-PSDrive -name script -PSProvider FileSystem -Root $HOME\Documents\Scripts
New-Alias -Name sudo 'script:\sudo.ps1'

The first line mounts the scripts directory as “script:", the other alias a script for sudo emulation that we’ll add now:

> notepad $HOME\Documents\Scripts\sudo.ps1

Put that inside:

$file, [string]$arguments = $args;
if([System.IO.File]::Exists("$(get-location)\$file"))
{
        $file = "$(Get-Location)\$file";
}
$psi = new-object System.Diagnostics.ProcessStartInfo $file;
$psi.Arguments = $arguments;
$psi.Verb = "runas";
[System.Diagnostics.Process]::Start($psi);

Restart the shell as admin (right-click: run as admin) and type that:

> Set-ExecutionPolicy RemoteSigned

Answer yes of course, this will let you run scripts that you created on your local system. Restart PowerShell again (normally this time) and you’re good to go.

To run or edit stuff with elevated privileges the syntax will of course be “sudo gvim file” for example.

Updated:

Comments