0

I'm writing a script that has a lot of output, and can take multiple computer names. The output announces the computer name, then a lot of info about that particular computer. I want to have a series of #s above and below where it announces the computer name before each section of info, but would like to see if I can have the amount of #s be the same as the length of the provided computer name(s). For example:

########
COMPNAME
########

or

##############
LONGERCOMPNAME
##############

I'd rather not have to have an if else for every possible case, such as

if ($compname.length -eq "8") {
  Write-Host "########"
  Write-Host "$compname"
  Write-Host "########"
} elseif ($compname -eq "9") {
  Write-Host "#########"
  Write-Host "$compname"
  Write-Host "#########"

and so on. If I have to, I will, it'd just be ten of those or so. Or I could just use some amount of #s that will always definitely cover at least the max length a computer name might be.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Casspers
  • 15
  • 1
  • 3

4 Answers4

6

You're gonna love this feature of PowerShell. You can "multiply" a string.

Try this:

$sep = '@'

Write-Output ($sep*5)

$names = "Hello World", "me too", "goodbye"

$names | % {
Write-Output ($sep*($_.Length))
Write-Output $_
Write-Output ($sep*($_.Length))
}

OUTPUT

@@@@@
@@@@@@@@@@@
Hello World
@@@@@@@@@@@
@@@@@@
me too
@@@@@@
@@@@@@@
goodbye
@@@@@@@
Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • Thank you, Kory! This is exactly what I was looking for. I've changed my mind a little about the formatting I want to use, but this answers my question perfectly. – Casspers Feb 06 '17 at 16:55
0

you can do this

$NbChar=5


#method 1 (best)
'@' * $NbChar

#method 2
New-Object  System.String "@", $NbChar

#method 3
-join (1..$NbChar | %{"@"})

#method 4
"".PadLeft($NbChar, '@')
Esperento57
  • 16,521
  • 3
  • 39
  • 45
0

I'd recommend wrapping Kory Gill's suggestion in a custom function, so that you have an easy way of formatting any given name:

function Format-ComputerName([string]$ComputerName) {
  $separator = '#' * $ComputerName.Length
  '{0}{1}{2}{1}{0}' -f $separator, [Environment]::NewLine, $ComputerName
}
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

Or a fixed-width banner:

"{0}`r`n# {1,-76} #`r`n{0}" -f ('#' * 80), $compname;

e.g.:

################################################################################
# LONGERCOMPNAME                                                               #
################################################################################

You can also add dates, times, Etc.:

"{0}`r`n# {1:G} : {2,-54} #`r`n{0}" -f ('#' * 80), (Get-Date), $compname;

e.g.:

################################################################################
# 04/02/2017 16:42:07 : LONGERCOMPNAME                                         #
################################################################################

More info on string formatting here

Simon Catlin
  • 2,141
  • 1
  • 13
  • 15