none
Tarting up my Menu Write-Host

    Question

  • Hi All

    I am attempting to tart up my menu with coloured text and foreground colours. I am trying to get 3 rows of the same foreground colour which I have achieved but it shows is like a table with blue boarders and looks a mess. I always use Powershell ISE.

    Write-host-ForegroundColorDarkMagenta-BackgroundColorwhite"                              "
    Write-host
    -ForegroundColorDarkMagenta-BackgroundColorwhite"     Powershell Project 1     "
    Write-host-ForegroundColorDarkMagenta-BackgroundColorwhite"                              "

    It looks ok when I google search for screen shot examples. Any ideas?

    Thursday, April 28, 2016 2:38 PM

Answers

  • Ah..., thanks Mike. I tried my (newer) below example in PowerShell 2.0, just to make sure it worked. Does this work for either you, Mike, or the OP? What this function does is change the console's foreground and background color long enough to echo the lines. When done, it reverts is back to the foreground and background colors that were being used before.

    Function Show-ProjectNumber {
        $ConsoleFore = [System.Console]::ForegroundColor
        $ConsoleBack = [System.Console]::BackgroundColor
        [System.Console]::ForegroundColor = 'DarkMagenta'
        [System.Console]::BackgroundColor = 'White'
    
        Write-OutPut -InputObject '                          '
        Write-Output -InputObject '   PowerShell Project 1   '
        Write-OutPut -InputObject '                          '
    
        [System.Console]::ForegroundColor = $ConsoleFore
        [System.Console]::BackgroundColor = $ConsoleBack
    }
    Show-ProjectNumber

    This function is a good candidate for being an advanced function. It's clear which sections would fall into the Begin, Process, and End blocks. You can also add parameters and pass in what you would want it to "say," as opposed to hard-coding in "PowerShell Project 1," as well. This is of course, if it works on other computers. Since the word to print would be dynamic, you'd have to dynamically create the how big you'd make the lines above and below the word to print. If someone gets crazy, and does this (maybe me in a new blog post), you can borrow some of what I've done here, http://tommymaynard.com/quick-learn-add-a-dynamic-number-of-asterisks-2015, where I dynamically added asterisks.

    Edit: Explained what the function does a litte.




    Thursday, April 28, 2016 6:03 PM

All replies

  • Hi,

    Either don't set the background color or set the entire script pane to use white as the color.


    Thursday, April 28, 2016 2:50 PM
    Moderator
  • It's too bad you didn't include an image of what this "table with blue boarders" looks like, as I'm having a difficult time trying to picture it. Anyway, perhaps what I've done in the images below will help you. The first image allows you to see the whitespace indicators inside the here-string -- the part that is being assigned to the $String variable. Good luck.

    Thursday, April 28, 2016 3:55 PM
  • Tommy - here's what it looks like:


    EDIT: Same result with a here-string too:


    Thursday, April 28, 2016 4:09 PM
    Moderator
  • Ah..., thanks Mike. I tried my (newer) below example in PowerShell 2.0, just to make sure it worked. Does this work for either you, Mike, or the OP? What this function does is change the console's foreground and background color long enough to echo the lines. When done, it reverts is back to the foreground and background colors that were being used before.

    Function Show-ProjectNumber {
        $ConsoleFore = [System.Console]::ForegroundColor
        $ConsoleBack = [System.Console]::BackgroundColor
        [System.Console]::ForegroundColor = 'DarkMagenta'
        [System.Console]::BackgroundColor = 'White'
    
        Write-OutPut -InputObject '                          '
        Write-Output -InputObject '   PowerShell Project 1   '
        Write-OutPut -InputObject '                          '
    
        [System.Console]::ForegroundColor = $ConsoleFore
        [System.Console]::BackgroundColor = $ConsoleBack
    }
    Show-ProjectNumber

    This function is a good candidate for being an advanced function. It's clear which sections would fall into the Begin, Process, and End blocks. You can also add parameters and pass in what you would want it to "say," as opposed to hard-coding in "PowerShell Project 1," as well. This is of course, if it works on other computers. Since the word to print would be dynamic, you'd have to dynamically create the how big you'd make the lines above and below the word to print. If someone gets crazy, and does this (maybe me in a new blog post), you can borrow some of what I've done here, http://tommymaynard.com/quick-learn-add-a-dynamic-number-of-asterisks-2015, where I dynamically added asterisks.

    Edit: Explained what the function does a litte.




    Thursday, April 28, 2016 6:03 PM
  • I published my first module to the PowerShell Gallery thanks to this post. Not only does it better solve the problem of the thread creator, it gives Write-Output the ability to use foreground and background colors in the console host without the need to use Write-Host. Here's a link to my blog post about the project which includes information on how to download it. I added a good amount of images so you can see the module's functions in action.
    Friday, May 13, 2016 5:23 AM
  • Sorry all

    Been extremely busy and havent been able to check tech net in a while. This is perfect! the result of what I was hoping to achieve many thanks. The 3rd post down with the blue lines in between rows. I have read the blog and very nice :)

    Friday, May 13, 2016 2:57 PM