Change prompt in Powershell

I’ve always really stuck to WSL and Debian on my Windows machines however the Linux implementation on Windows has started to really suck these days. Because I am forced to work from Powershell more, I figured it’s time to create a multi-line prompt that let’s me display the ludicrously long Windows directory structures (thanks, OneDrive), while still having a good amount of space for writing very long commands such as batect build-container.

In later reading, I realized that it’s a little odd that I reference my OneDrive. The company had everything stored on OneDrive, which caused issues with Git and I had to move off of it. But that’s the context about why my $profile was on OneDrive; because everything was on OneDrive.

Dealing with Blocked Scripts

Windows has this really annoying (probably good though) feature where it blocks execution of pretty much all scripts.

One way to get around this is to disable your security. Another way to get around this is to allow RemoteSigned however unfortunately this did open my machine up to some security issues.

I can write a script called “somescript.ps1” and execute it fine. This is not what I want; I want the be able to explicitly ‘trust’ certain scripts and not trust everything else.

Until I figure this out, the way that I got around my Profile being blocked was to run:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

This was the best balance between security and openness that I could find.

Getting to the profiles

Powershell uses “profiles” and the “prompt” can be set inside of the “profile”. This is, I guess, equivalent to .bashrc. To change the “profile”. You can find a list of the locations of your profiles here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.2#the-profile-files

I just wanted to do this for my user, even though I’m the only user of this desktop, so I just ran echo $profile and created the directory path becase it didn’t exist in my OneDrive.

Then I just ran code $profile and updated it with the following ‘code’

function profile {
    Write-Host ("Nice: ")
}

Saving this presents us with the following “prompt” in WT:

nice:
PS>

So there is a trick to this which took me a second to realize; your function needs to return a String in order to replace that “PS>”. Easiest way to put this:

function profile {
    Write-Host ("My Shell")
    return " "
}

This will show “My Shell” and allow input under it. If you want it to be a single line then you can add the -nonewline flag to Write-Host.