Home > OS >  Changing PowerShell default colors not saved after restart, set readable colors permanently?
Changing PowerShell default colors not saved after restart, set readable colors permanently?

Time:07-13

The default PowerShell colors aren't very good imho and I don't mean "the colors aren't to my taste" but rather they simply aren't readable.

By default the errors, strings and comments are using dark-blue or dark cyan on a black background which is hard to read. Seem like a weird choice to me, because that's just bad practice for UI imho.

PowerShell default colors are unreadable

I know I can "fix" it using the following commands:

# make strings readable
$colors = @{}
$colors['String'] = [System.ConsoleColor]::Cyan
Set-PSReadLineOption -Colors $colors

# make error messages readable
$host.privatedata.ErrorBackgroundColor = 'DarkRed'
$host.privatedata.ErrorForegroundColor = 'White'

However this isn't saved as the new default. When I close and open PowerShell it's back to the crappy unreadable colors again. Btw I know the newer PowerShell versions have more options for color schemes, but due to circumstances out of my control we have to work with PowerShell 5.

Is there a way to change the default colors, have it saved after restart so that the new colors are permanent? Preferably for all users, if possible.

CodePudding user response:

Ok so if I understand correctly the color settings aren't stored, but you can set them using the profile script. A profile is nothing more than a script that is run everytime PowerShell starts, and it's specific for a user. The expected location for the profile script is somewhere in C:\Users\<username> folder (aka %HOMEPATH%), and you can see the expected file location by typing:

$profile

This is just the expected location, so it's possible the file or even the folder doesn't exist. If it doesn't exist, you'll have to create it by hand, and then you can open the file by typing:

invoke-item $profile

And then paste the contents below, save it, and restart PowerShell

# make strings readable
$colors = @{}
$colors['String'] = [System.ConsoleColor]::Cyan
$colors['Comment'] = [System.ConsoleColor]::Gray
Set-PSReadLineOption -Colors $colors

# make error messages readable
$host.privatedata.ErrorBackgroundColor = 'DarkRed'
$host.privatedata.ErrorForegroundColor = 'White'

Or alternatively
you can do all the above steps in one go, by running the code below

# check if profile script exists
if (!(Test-Path $profile))
{
   # create folder if needed
   New-Item -ItemType Directory -Force -Path (Split-Path -Path $profile)

   # write string to create file using Out-File
   "# make strings readable
`$colors = @{}
`$colors['String'] = [System.ConsoleColor]::Cyan
`$colors['Comment'] = [System.ConsoleColor]::Gray
Set-PSReadLineOption -Colors `$colors

# make error messages readable
`$host.privatedata.ErrorBackgroundColor = 'DarkRed'
`$host.privatedata.ErrorForegroundColor = 'White'" | Out-File -FilePath $profile -Encoding "UTF8"

   # profile script will run when PowerShell starts
   # but also run it once now
   . $profile
}

That's quite a lot just to set the default colors, but, whatever works works.

CodePudding user response:

@Mathias tells you in the comments. You want to

invoke-item $profile

Then add your code in, save, then restart your terminal. That should do it.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles

  • Related