Finally, The new version of PowerShell has released in about three years after PowerShell Core 6 released. Many PowerShell fans have been waiting for the latest version, including me. The new PowerShell 7 minimizing the gap between Windows PowerShell 5 and cross-platform version of PowerShell Core 6. Also, more powerful features added to the latest version.

I want to share some tips who wish to configure the PowerShell environment more powerful, but yet friendly.

oh-my-posh

Inspired by oh-my-zsh, oh-my-posh brings powerful console indicators and features to your PowerShell session.

In PowerShell Core 6, PSReadLine was not a built-in module. So before you install the oh-my-posh, you should install the PSReadLine module first. This additional step makes somewhat cumbersome. But in PowerShell 7, PSReadLine included default distribution, so you can just set up oh-my-posh directly.

You can run these commands to configure oh-my-posh for PowerShell.

Install-Module posh-git -Scope CurrentUser

Install-Module oh-my-posh -Scope CurrentUser

Next, enable your prompt and change the theme.

Set-Prompt

Set-Theme Paradox

Note: Your default terminal font may not render powerline characters correctly at first time. Please lookup https://github.com/ryanoasis/nerd-fonts repository and choose your favorite powerline patched font. Also, if you are looking for CJK balanced font, please consider using the D2Coding font (https://github.com/naver/d2codingfont) from Naver.

Then, open your PowerShell profile script with vim $PROFILE command and paste the below codes.

Import-Module posh-git Import-Module oh-my-posh

Set-Theme Paradox

These code makes your default PowerShell look and feels more improved. You can change the theme of what you want.

PSFzf Module

As you may know, fzf gives you powerful search features for many scenarios. Especially fzf shines when it combined with history search and key binding with Ctrl + R key combination.

PSFzf precisely addresses this scenario. Also, the fzf has written in Golang. So you can use the fzf in macOS, Linux, and even in Windows.

To use the PSFzf module, you should install the fzf on your machine first.

On Windows, you can choose between the Chocolatey Package Manager or Scoop Package Manager.

On Linux, most package manager provides the fzf package. (Except Ubuntu does not offer the package. You can use the Git installation method alternatively.)

On macOS, Homebrew is your best friend.

After you installed the fzf binary, simply install the PSFzf module into your PowerShell.

Install-Module PSFzf -Scope CurrentUser

Finally, let PSFzf overrides default key binding at startup. Paste the below codes.

Remove-PSReadlineKeyHandler 'Ctrl+r'

Remove-PSReadlineKeyHandler 'Ctrl+t'

Import-Module PSFzf

PowerShell version of k, kubectx, and kubens

If you are stick with Kubernetes every day, you may know or consider shorthand expression of the kubectl.

I’m using the alias K , kubectx and kubens every day. But these scripts are not designed for PowerShell. So I tried to make the own version of each shorthand convention. Naturally, you can use below snippets to your other environments, including Windows.

The “K” Alias

Set-Alias k kubectl

The “Select-KubeContext” (a.k.a. kubectx)



[CmdletBinding()]

[Alias('kubectx')]

param (

[parameter(Mandatory=$False,Position=0,ValueFromRemainingArguments=$True)]

[Object[]] $Arguments

)

begin {

if ($Arguments.Length -gt 0) {

$ctx = & kubectl config get-contexts -o=name | fzf -q

} else {

$ctx = & kubectl config get-contexts -o=name | fzf

}

}

process {

if ($ctx -ne '') {

& kubectl config use-context $ctx

}

}

} function global:Select-KubeContext {[CmdletBinding()][Alias('kubectx')]param ([parameter(Mandatory=$False,Position=0,ValueFromRemainingArguments=$True)][Object[]] $Argumentsbegin {if ($Arguments.Length -gt 0) {$ctx = & kubectl config get-contexts -o=name | fzf -q @Arguments } else {$ctx = & kubectl config get-contexts -o=name | fzfprocess {if ($ctx -ne '') {& kubectl config use-context $ctx

The “Select-KubeNamespace” (a.k.a. kubens)



[CmdletBinding()]

[Alias('kubens')]

param (

[parameter(Mandatory=$False,Position=0,ValueFromRemainingArguments=$True)]

[Object[]] $Arguments

)

begin {

if ($Arguments.Length -gt 0) {

$ns = & kubectl get namespace -o=name | fzf -q

} else {

$ns = & kubectl get namespace -o=name | fzf

}

}

process {

if ($ns -ne '') {

$ns = $ns -replace '^namespace/'

& kubectl config set-context --current --namespace=$ns

}

}

} function global:Select-KubeNamespace {[CmdletBinding()][Alias('kubens')]param ([parameter(Mandatory=$False,Position=0,ValueFromRemainingArguments=$True)][Object[]] $Argumentsbegin {if ($Arguments.Length -gt 0) {$ns = & kubectl get namespace -o=name | fzf -q @Arguments } else {$ns = & kubectl get namespace -o=name | fzfprocess {if ($ns -ne '') {$ns = $ns -replace '^namespace/'& kubectl config set-context --current --namespace=$ns

Paste these codes to your $PROFILE script code and you will get more comfortable PowerShell workspace for DevOps.

Conclusion

I think the real power of PowerShell is, you can write your shell automation code more readable, manageable, and reusable for every operating system, including Windows. It means that no more corner-case considerations are needed. You can just write your PowerShell snippet once and share the code everywhere.

More and more features will arrive at the next version of PowerShell. I can’t wait for that!