Built on the .NET Framework, Windows PowerShell is a task-based command-line shell and scripting language; it is designed specifically for system administrators and power-users, to rapidly automate the administration of multiple operating systems (Linux, macOS, Unix, and Windows) and the processes related to the applications that run on those operating systems.
A big difference between other scripting languages and Powershell is that it is fully object-based and not text-based. Therefore it is important to keep in mind that what you might see as output on your screen is only a representation of the object, but not the object itself.
Lately, Microsoft has even made PowerShell open source with their PowerShell Core iteration which is cross-platform (Windows, Linux, and macOS) allowing you to take full advantage of automation and configuration tool/framework that works well with your existing tools and is optimized for dealing with structured data (e.g. JSON, CSV, XML, etc.), REST APIs, and object models. It includes a command-line shell, an associated scripting language and a framework for processing cmdlets.
In this post I will explain the basic steps to set up a what I would call a “sane” working environment, which arguably gives you an experience similar to a Bash shell.
In specific I will discuss the following tools/modules:
- ConEmu
- Environment Settings
- Powershell Profile
ConEmu
Like many other scriptwriters and developers, I spend quite a bit of time in command line applications (Windows CMD, PowerShell, Terminal on MacOS, etc.). Unfortunately, these applications don’t offer a lot in terms of customization. ConEmu is a console emulator with tabs and panes, which is great for those who want easier multi-tasking. It’s a high customization, tabbed console emulator that lets you run any shell you want.
Install ConEmu
First step is to download the latest version of ConEmu. Pick the installer for either the latest preview or stable version.
Run the installer, choose the 32-bit or 64-bit option (depending on which version of Windows you have installed), and keep all the default options.
Configure ConEmu
Once installed, start it up. The first time you run it, you’ll be presented with a fast configuration screen. Everything here can be changed later but it’s a good place to start.
For usage with PowerShell I use the following settings in ConEmu:
- Startup
- Tasks
- Select Item 5: {Shells::Powershell}
- In the Commands field I set my environment to:
- “C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe -new_console:d:F:\GitHub\PowerShell”
- Tasks
- Environment > “Set up environment variables” = “set HOME=F:\GitHub\PowerShell”
- Features
- Colors
- <Monokai>
- Colors
When we break down the above settings, we are ultimately telling ConEmu to ensure that a new shell or tab is spawned with the starting directory that contains my PowerShell scripts, and that ConEmu is able to run scripts that reside under this path.
Environment Settings
The PSModulePath environment variable stores the paths to the locations of the modules that are installed on disk. Windows PowerShell uses this variable to locate modules when the user does not specify the full path to a module. The paths in this variable are searched in the order in which they appear.
When Windows PowerShell starts, PSModulePath is created as a system environment variable with the following default value: $home\Documents\WindowsPowerShell\Modules; $pshome\Modules.
Only one environment variable should be set for Powershell, specifically the “PSModulePath” variable. This variable allows the usage of modules (and functions within these modules) straight from any Powershell CLI. It doesn’t matter whether it is powershell.exe, Powershell ISE, or a custom execution from a different path.
To set the variable go the following:
1 2 3 4 5 6 |
# get current path $curpath = [Environment]::GetEnvironmentVariable("PSModulePath") # update path with new path [Environment]::SetEnvironmentVariable( "PSModulePath", "F:\GitHub\PowerShell\odules;" + $curpath) # check if settings were updated correctly get-childitem env:psmodulepath |
Source: Microsoft TechNet
PowerShell Profile
If you find yourself using it as a command line shell it may be useful to store your functions and customization’s in a profile that gets loaded every time you load the console. PowerShell also allows you to specify a set of commands which will run before spawning a new shell. This is convenient for pre-loading modules, setting aliases and setting the path of the new shell.
Typically, (especially if you are doing this for the first time on your workstation) there is not a profile file created that pre-loads modules, etc. But to be safe, I would run the following cmdlet to verify. If it comes back as False, then you know you will need to create one:
1 |
Test-Path $Profile |
If one has been created already for some reason, and you want to create new one right away with no regard to the existing profiles specifications you can run:
1 |
New-Item –Path $Profile –Type File –Force |
Once the file has been created, opening up your favorite text editor *Cough* VS Code w/ PowerShell Extension *Cough* and enter something on the lines of:
1 2 3 4 5 6 7 8 9 10 11 12 |
# PowerShell Profile # Set Shell Path $env:Path = "F:\GitHub\PowerShell" # Import Modules Import-Module MSOnline Import-Module Azure # Set Command Aliases Set-Alias cls clear Set-Alias ls dir |
Summary
Now we are talking! Playing around with the settings I cleaned up the default look of the console’s default settings and tabs to give it a nice clean look while maintaining the search menu in the top right hand side of the shell:
There are plenty more tweaks you could do to tailor ConEmu to your preferences (the full documentation can be found here), but I find these settings are good starting point for creating a sleek-looking, effective command line application that is light years ahead in terms of flexibility and customization for your PowerShell command line adventures.
Inspired by: Sudesh JetHoe & Mike Larah