Answered by:
Using Write-Host to display a hashtable

Question
-
These would seem to be a very simple task. Display a hashtable's contents in red.
Hashtable contents: PS F:\Project> $n Name Value ---- ----- Tennessee Nashville Ohio Columbus New York Albany My attempt to format the output and the results: PS F:\Project> Write-Host $n -ForegroundColor Red System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry
I've tried using an enumerator and just about every clever trick I can think of. How can I get the output of the hashtable to display as red text?Something tells me there's an obvious solution that I'm overlooking...
I'm the most humble person you've ever met.Wednesday, December 28, 2011 4:18 PM
Answers
-
$ht = @{} $ht.Add('Tennessee' , 'Nashville') $str = $ht | Out-String Write-Host $str -ForegroundColor red
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')- Marked as answer by James Keeler Wednesday, December 28, 2011 6:47 PM
Wednesday, December 28, 2011 4:53 PM -
Or just
$ht = @{} $ht.Add('Tennessee' , 'Nashville') $ht.Add('New-York' , 'Brooklyn') $ht.Add('California' , 'Bollywood') Write-Host ($ht | Out-String) -ForegroundColor Red
Karl
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})- Marked as answer by James Keeler Wednesday, December 28, 2011 6:48 PM
Wednesday, December 28, 2011 6:00 PM -
$lines = ($n | out-string) -split "`n" write-host $lines[0..2] write-host $lines[2..($lines.count -1)] -foregroundcolor "red"
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "- Marked as answer by James Keeler Wednesday, December 28, 2011 6:47 PM
Wednesday, December 28, 2011 4:39 PM
All replies
-
Try this:
Write-Host $n.Values -ForegroundColor Red
jv- Edited by jrv Wednesday, December 28, 2011 4:24 PM
Wednesday, December 28, 2011 4:20 PM -
I'd like to keep the same, default layout of the hashtable, i.e. Name column with keys and Value column with values.
Here's the reason: My real hashtable contains error objects for a list of computers. Each computer name is a key and the error is it's value. Since they're errors I'd like them to be red.
I'm the most humble person you've ever met.Wednesday, December 28, 2011 4:26 PM -
$lines = ($n | out-string) -split "`n" write-host $lines[0..2] write-host $lines[2..($lines.count -1)] -foregroundcolor "red"
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "- Marked as answer by James Keeler Wednesday, December 28, 2011 6:47 PM
Wednesday, December 28, 2011 4:39 PM -
$ht = @{} $ht.Add('Tennessee' , 'Nashville') $str = $ht | Out-String Write-Host $str -ForegroundColor red
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')- Marked as answer by James Keeler Wednesday, December 28, 2011 6:47 PM
Wednesday, December 28, 2011 4:53 PM -
If you do this you willsee that this really doesn't work/
$ht = @{}
$ht.Add('Tennessee' , 'Nashville')
$ht.Add('New-York' , 'Brooklyn')
$ht.Add('California' , 'Bollywood')
Write-Host ($ht.Keys + " " +$ht.Values) -ForegroundColor Red
jvWednesday, December 28, 2011 4:59 PM -
If you do this you willsee that this really doesn't work/
$ht = @{}
$ht.Add('Tennessee' , 'Nashville')
$ht.Add('New-York' , 'Brooklyn')
$ht.Add('California' , 'Bollywood')
Write-Host ($ht.Keys + " " +$ht.Values) -ForegroundColor Red
jv
Yes, that's why I deleted that post, and re-posted. This works:$ht = @{}
$ht.Add('Tennessee' , 'Nashville')
$ht.Add('New-York' , 'Brooklyn')
$ht.Add('California' , 'Bollywood')
$str = $ht | Out-String
Write-Host $str -ForegroundColor Red
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')Wednesday, December 28, 2011 5:03 PM -
Or just
$ht = @{} $ht.Add('Tennessee' , 'Nashville') $ht.Add('New-York' , 'Brooklyn') $ht.Add('California' , 'Bollywood') Write-Host ($ht | Out-String) -ForegroundColor Red
Karl
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})- Marked as answer by James Keeler Wednesday, December 28, 2011 6:48 PM
Wednesday, December 28, 2011 6:00 PM -
Or just
$ht = @{} $ht.Add('Tennessee' , 'Nashville') $ht.Add('New-York' , 'Brooklyn') $ht.Add('California' , 'Bollywood') Write-Host ($ht | Out-String) -ForegroundColor Red
KarlQuite so, Karl.
I only include intermediate variables in my answers where I think it will make my intent clearer.
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')Wednesday, December 28, 2011 6:21 PM -
Karl wins however there is no excuse for mistaking the capital of California.
jvWednesday, December 28, 2011 6:29 PM -
Well, I know it's not Bollywood. That was your invention, jv. I honestly don't know what the capital of California is (without looking). Is it L.A.?
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')Wednesday, December 28, 2011 6:52 PM -
Sacramento
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "Wednesday, December 28, 2011 6:57 PM -
Sacramento
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Thanks, Rob. This could be called "off-topic", but I like a crowd with a sense of humour.
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')Wednesday, December 28, 2011 7:04 PM -
Thanks, Rob. This could be called "off-topic", but I like a crowd with a sense of humour.
In that case, the answer is "leaving for Texas".
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "Wednesday, December 28, 2011 7:09 PM -
Or just
$ht = @{} $ht.Add('Tennessee' , 'Nashville') $ht.Add('New-York' , 'Brooklyn') $ht.Add('California' , 'Bollywood') Write-Host ($ht | Out-String) -ForegroundColor Red
KarlQuite so, Karl.
I only include intermediate variables in my answers where I think it will make my intent clearer.
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
Oh, I will keep that in mind - I've been trying to be less verbose lately (since someone said "I've learned a lot from Karl, but not brevity") ;)
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})Wednesday, December 28, 2011 8:19 PM