All Projects → mikemaccana → Powershell Profile

mikemaccana / Powershell Profile

Mike's Powershell Profile (and how to set up Windows console if you've been using *nix for 20 years)

Programming Languages

powershell
5483 projects

Labels

Projects that are alternatives of or similar to Powershell Profile

autoconf-archive
A mirror of the GNU Autoconf Archive, a collection of more than 500 macros for GNU Autoconf that have been contributed as free software by friendly supporters of the cause from all over the Internet.
Stars: ✭ 70 (-78.4%)
Mutual labels:  unix
Maurice Bach Notes
Notes on the classic book : The Design of the UNIX Operating System
Stars: ✭ 285 (-12.04%)
Mutual labels:  unix
Gwa tutorial
A comprehensive tutorial about GWAS and PRS
Stars: ✭ 303 (-6.48%)
Mutual labels:  unix
Ricing
a book about ricing unix-like systems
Stars: ✭ 257 (-20.68%)
Mutual labels:  unix
Melang
A script language of preemptive scheduling coroutine in single thread
Stars: ✭ 273 (-15.74%)
Mutual labels:  unix
Dotfiles
Configuration files I use on my main machine
Stars: ✭ 299 (-7.72%)
Mutual labels:  unix
cam-track
Windows, Unix, Raspberry Pi Computer python program to Track Camera X, Y Movements and Convert to Camera Pointing Position. Useful for Stabilization or Robotics Course Correction
Stars: ✭ 27 (-91.67%)
Mutual labels:  unix
Unix As Ide
The ebook version of Tom Ryder's series on the Unix programming environment
Stars: ✭ 315 (-2.78%)
Mutual labels:  unix
Data Science At The Command Line
Data Science at the Command Line
Stars: ✭ 3,174 (+879.63%)
Mutual labels:  unix
Augur
Python library and web service for Open Source Software Health and Sustainability metrics & data collection.
Stars: ✭ 304 (-6.17%)
Mutual labels:  unix
Unix History Make
Code and data to create a git repository representing the Unix source code history
Stars: ✭ 258 (-20.37%)
Mutual labels:  unix
Masterkey
secure interactive password manager with xchacha20poly1305, argon2id, and Go
Stars: ✭ 271 (-16.36%)
Mutual labels:  unix
Tucl
The first-ever paper on the Unix shell written by Ken Thompson in 1976 scanned, transcribed, and redistributed with permission
Stars: ✭ 303 (-6.48%)
Mutual labels:  unix
Cloudlibc
CloudABI's standard C library
Stars: ✭ 254 (-21.6%)
Mutual labels:  unix
Ir Rescue
A Windows Batch script and a Unix Bash script to comprehensively collect host forensic data during incident response.
Stars: ✭ 311 (-4.01%)
Mutual labels:  unix
4bsd-uucp
Simulate a UUCP network with 4.3BSD SimH images
Stars: ✭ 28 (-91.36%)
Mutual labels:  unix
Ngircd
Free, portable and lightweight Internet Relay Chat server
Stars: ✭ 292 (-9.88%)
Mutual labels:  unix
Proftpd
ProFTPD source code
Stars: ✭ 318 (-1.85%)
Mutual labels:  unix
Purse
GPG asymmetric (YubiKey) password manager
Stars: ✭ 313 (-3.4%)
Mutual labels:  unix
Wipe Modules
🗑️ Easily remove the node_modules folder of non-active projects
Stars: ✭ 304 (-6.17%)
Mutual labels:  unix

Mike's Powershell profile: how to set up a terminal in Windows if you've been using *nix for 20 years

Heya. I've been using bash for about two decades before getting into Powershell. I'm a *nix person. I've worked at both Red Hat in the early days, IBM's dedicated Linux team, and a bunch of other places working with *nix as an sysadmin, SRE, Architect, and CTO. I now develop node and TypeScript while trying to make verifying companies for EV HTTPS less painful at CertSimple.

If you come from a Linux or Unix background, and want to use Powershell properly, this is the right place.

  • Better defaults, like UTF-8 output, long history, better tab completion, adding missing drives for registry hives, and a nice short prompt using the '~' character. See defaults.ps1
  • Implementations of popular Unix commands. See unix.ps1
  • Useful commands for software development using git. See development.ps1
  • Useful commands and settings for node development. See node.ps1
  • Useful commands for crypto, including converting between different crypto file types, checking if private keys and certs match, etc. See openssl.ps1
  • Config files for Windows Terminal, Terminus, and other modern Windows terminals.

The profile code itself contains useful implementations of common patterns - eg, installing packages, reading the registry, interacting with files and processes. Learning the things required to make a profile you're happy with is a great way to get comfortable with Powershell.

The details below are minimal, but the names of most commands make things fairly obvious.

Prerequisities for any *nix user who wants to use Powershell

This is what I install on any Windows 10 box.

Powershell Core 6.2

Powershell Core 6.2 has a number of useful bits, but the main thing is it starts way faster than Powershell 5, so there's less lag when you open a new tab.

Powershell 6.2 includes PSReadline, which provides history with up/down arrows, ctrl left and right to move between words, and other useful keybindings you'll know from bash.

Trust PSGallery

To allow you to install items without further prompts:

Set-PSRepository -name PSGallery -InstallationPolicy Trusted

For less, Add-PAthVariable and a bunch of other useful commands that should be there out of the box

Get the Powershell Community Extensions. Run:

Install-Module Pscx -Scope CurrentUser

AllowClobber is needed due to this bug

For 'Remove-ItemSafely' - ie, trashing files from the command line

Run:

Install-Module -Name Recycle -Scope CurrentUser

For a decent, tabbed terminal

Tabbed terminals that work now

The terminals below all support:

  • Tabs
  • Readline
  • Right click paste
  • Copy on select

....and all the usual things you expect from any decent terminal emulator. I use Windows Terminal, which is in Preview right now, but usable as a day to day terminal. Fluent Terminal also works great out of the box.

  • Microsoft's official Windows Terminal. Open the Settings file, enable copyOnSelect and change "commandline": "C:\\Program Files\\PowerShell\\6\\pwsh.exe -nologo", - the nologo makes Powershell skip some boring startup messages.
  • FluentTerminal is a native Windows 10 terminal that feels as if Microsoft had written it. It requires minimal configuration (just to add Powershell 6 to the profiles), has all the features you'd expect, and is fast.
  • Terminus works great. Tweaking colors, keyboard shortcuts etc is easy via the menus, and my settings file is included if you just want my config.
  • Hyper Install Hyper 3 and modify the config to set:
shell: "C:\\Program Files\\PowerShell\\6\\pwsh.exe",

and

shellArgs: [],

To work around issues with arrow keys you'll also need to click Edit, Preferences, find keymaps, and replace the existing entries with one below:

  keymaps: {
    // Example
    // 'window:devtools': 'cmd+alt+o',
    "tab:new": "ctrl+t",
    // Also known as 'close tab'
    "pane:close": "ctrl+w",
    // This is a poor default, as these are used to navigate between words
    // "tab:next": ["ctrl+right"],
    // "tab:prev": ["ctrl+left"],
    // Bug workaround for https://github.com/zeit/hyper/issues/2873
    "editor:movePreviousWord": "",
    "editor:moveNextWord": ""
  }

Minimal terminals

The following apps are console window only - they don't provide tabs, graphical config tools, etc. You can add an app like Groupy to them to make a tabbed terminal, but they require more setup than just using Terminus.

  • The inbuilt Powershell 6 terminal
  • Alacritty is fast. You'll also need to have the following in AppData\Roaming\alacritty\alacritty.yml
    shell:
        program: 'C:\Program Files\PowerShell\6\pwsh.exe'
   
    enable_experimental_conpty_backend: true

Terminal apps that don't yet work on Windows

The apps below all plan on having Windows support in future, but don't yet properly work at the time of writing. There are links to the tracking bugs below.

Not a terminal

cmder's website makes it seems like it's a new terminal, but cmder is just ConEmu and some additional things you may already have installed and some other things you don't want (like cmd tools).

Old-style Win32 apps

These have the 'everything at once' UI design of older Windows operating systems.

  • ConEmu works, but has some contrast issues which make it hard to see the open tab, and is hampered by its author's desire for Windows XP support.
  • ConsoleZ is an updated version of the now-unmaintained Console2.

To pick a color scheme / theme

Browse iterm2colorschemes.com and download the equivalent named Windows Terminal file from their Windows Terminal color schemes. Open Settings and paste it into the schemes section, then select that scheme name for the profile you want to use it with in profiles.

Terminus has it's own color schemes, just open Settings > Appearance > Color Scheme and pick one (or use my config file).

ColorTool also supports importing the well know .itermcolors format. Download ColorTool from Microsoft's GitHub. You can edit an .itermcolors file using terminal.sexy.

Run:

./colortool -b color-scheme.itermcolors

For OpenSSH

OpenSSH now comes with Windows. Settings -> Manage Optional Features -> OpenSSH client.

For a nice prompt

By default $prompt is a minimal, bash-style prompt with a truncated path. If you're feeling fancier, you can install oh-my-posh.

For OpenSSL (if you need it)

Personally I use OpenSSL for viewing private keys, pubkeys, certificates, and other TLS/PKI work. Unless you do the same you probably don't need OpenSSL.

The Windows version of OpenSSH uses Windows CryptoAPI rather than OpenSSL, so if you want to add OpenSSL, you'll have to install it.

Use this up to date, secure Windows OpenSSL build.

For host, dig and other DNS tools

Download Bind 9 for Windows. Extract the zip and run BINDinstall.exe as Administrator. Select 'Tools Only'.

If you want a better 'select-string' (aka grep)

The Find-String module adds match highlighting and other fun things. For me the inbuilt select-string is enough.

Install-Module -Name Find-String -Scope CurrentUser

If you use node

Start Powershell 5 as admin and run:

npm install -g windows-build-tools

Minimum Powershell concepts to learn before you rant about how much you hate Powershell

These come with powershell. If you don't know them you're the equivalent of someone who doesn't know grep ranting about how "Unix is like DOS". That might be painful to hear but it's true.

select (also called select-object) - select the fields you want on an object

get-member - show the properties and methods of an object

get-itemproperty - show the properties of registry objects (ls only shows children)

where (also called where-object) - choose items matching some criteria.

get-help some-command -examples - every command has examples.

How does Powershell actually differ from bash, day-to-day?

Here's a real comparison: a bash script used to find a compromised node module a little while ago:

find . -type d -name "eslint-scope" -print0 | xargs -n 1 -0 -I % sh -c "(cat %/package.json | npx json version) && echo '(at %)'"

Here's a Powershell version. This is written using Unix aliases as that's what folk are familiar with, though it's generally considered better to use the full names in your scripts:

$results = @{} 
ls -recurse -filter "eslint-scope" | foreach { 
	$file = "${PSItem}\package.json" 
	$version = cat $file | convertfrom-json | select -ExpandProperty version 
	$results.Add($file,$version) } 
echo $results | format-list

You might prefer one or the other, but the important difference:

  • Powershell has real objects. We're creating a hashmap of $file,$version pairs.
  • In Powershell we select the keys whose values we want, rather than scraping with regexs
  • Powershell seperates content from presentation, so we can format our $results however we want
  • Powershell has inbuilt tools to parse JSON (and CSV, and Excel, and other common formats). It can make them too.

Why Windows filesystem is slow

Filesystem access under Windows is undoubtably slower than ext3/4 for most tasks. See https://github.com/Microsoft/WSL/issues/873#issuecomment-425272829 for more details about why and some performance hints to speed things up. There's also plans to improve things in future.

Included commands

Stuff that should be there out of the box

edit - edits a file (using VSCode insiders, but modify as you please)

open - open a file using associated app

settings - the Windows Settings app

explorer - file explorer

File management

expand-archive - also called unzip

find-file

show-links

OS management

reboot

get-windows-build

disable-windows-search - Windows Search Indexer doesn't handle filesystems with lots of small files - the 'Windows Search Indexer' process will kick in when you're working, and make interactive so slow you'll be waiting for your keyboard strokes to appear on screen. I've discussed this with Rich Tuner from Microsoft who says the search team are working on fixing this, but right now I suggest you disable the indexer.

You can still search for files without the Indexer, it'll just not using the indexes, so take a little longer. You can also just use find-file included here.

get-serial-number

Unix like commands

grep

grepv

df

sed

edit-recursive - sed but recursive

fuser

pkill

pgrep

touch

file

sudo - note command after sudo must be quoted

uptime

cut

export

ln

pstree

which

find

Powershell stuff

prompt - a nice Unixlike prompt with ~ style truncation for the home directory

edit-powershell-profile

update-powershell-profile - re-run your profile

set-title - set the window title

get-path - get the PATH, one item per line

Development

get-git-ignored

get-git-untracked

gg - A git grep Alias

yarn - Yarn wrapper with yarn ls re-added, since I hate typing yarn list

sourcetree - SourceTree

Crypto

read-certificate

read-csr

read-rsa-key

read-rsa-key

read-ecc-key

read-pkcs12

test-openssl-client

convert-pem-to-p12

convert-p12-to-pem

convert-crt-to-pem

show-rsa-certificate-modulus

show-ecdsa-certificate-ppv-and-curve

show-rsa-key-modulus

show-ecc-key-ppv-and-curve

show-rsa-csr-modulus

protect-file

unprotect-file

convert-key-to-hpkp-pin

convert-website-to-hpkp-pin

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].