1

Suppose I have the following output that keeps outputting from loop:

Write-Host "`r`n $server | $db | success! | [$Time] "

The current output I get is like this:

output

Is there a way i can make this equivalently formatted?

so that it would end up more like this:

proper_output

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Cataster
  • 3,081
  • 5
  • 32
  • 79
  • 3
    you should create an object and output this either with a format cmdlet or just as it is ... Powershell will format it for you. – Olaf Feb 25 '19 at 23:41
  • @Olaf so something like this: $output = Write-host "`r`n $server | $db | success! | [$Time] "? if its just as it is, how would it be any different? – Cataster Feb 25 '19 at 23:43
  • 3
    No. Something like `[PSCustomobject]@{'Server'=$server; 'DB'=$db; 'Result'='success'; 'Time'=$time}`. – Ansgar Wiechers Feb 25 '19 at 23:44
  • May also be useful to check out the [Format-Table](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/format-table?view=powershell-6) cmdlet. – romellem Feb 25 '19 at 23:45
  • 1
    [Related](https://stackoverflow.com/a/40344479/1630171) – Ansgar Wiechers Feb 25 '19 at 23:45
  • @romellem thats what i considered initially but since its not really a table...i am not sure how this would be integrated – Cataster Feb 25 '19 at 23:46
  • 1
    Build. Custom. Objects. – Ansgar Wiechers Feb 25 '19 at 23:48
  • @AnsgarWiechers im reading through your post as we speak :) – Cataster Feb 25 '19 at 23:49
  • @AnsgarWiechers so the object, i.e. 'Server', i dont want to output that..only the value. can i discard those like 'Server', 'DB', etc? also, i noticed in your post that the output is being done backwards to frontwards. like c,e,d,b,a...wouldnt that ruin my ordering to begin with in my case? – Cataster Feb 25 '19 at 23:53
  • @AnsgarWiechers so i just used it in the script as pscustom object, asn besides the fact its outputting the property name (i.e server, db,time) which i was hoping it would be possibly to ignore them, its still not formatting equivalently. its outputting the exact same zigzag pattern as the current output i have in my post – Cataster Feb 26 '19 at 00:07
  • you can do it in two steps ... [1] get the length of the longest item in each column [2] use the `-f` string format operator with the alignment option to fit each into the needed space. this ... -f Format operator - PowerShell - SS64.com — https://ss64.com/ps/syntax-f-operator.html – Lee_Dailey Feb 26 '19 at 00:07
  • @Lee_Dailey how would i get the length though? i guess i can count what the current out put is...is that what you mean? – Cataster Feb 26 '19 at 00:08
  • You may take a step back and take some time to start to learn the very basics of Powershell. That would save you from a lot of missunderstandings, wasted time and frustration. It will even help you to understand the help you get here or in other forums. – Olaf Feb 26 '19 at 00:09
  • @Lee_Dailey oh i see. something like this: Left and right align text: PS C:\> "|{0,-10}| |{1,10}|" -f "hello", "world" |hello || world| – Cataster Feb 26 '19 at 00:14
  • @Cataster - yes, that is the idea. you will need to tweak it carefully, but it allows a great deal of control. – Lee_Dailey Feb 26 '19 at 00:26
  • @Cataster - you are quite welcome! glad to help a bit ... [*grin*] – Lee_Dailey Feb 26 '19 at 00:51

1 Answers1

1

thanks to @Lee_Dailey, I was able to get the formatting!

I used the -f operator:

"`r`n {0,1} | {1,-32} | {2,-20} | {3,1} " -f "$server","$db","success!","[$Time]"

https://ss64.com/ps/syntax-f-operator.html

Cataster
  • 3,081
  • 5
  • 32
  • 79
  • 1
    ha ha . I liked this approach although customobjects are better. :) – Ranadip Dutta Feb 26 '19 at 10:49
  • 1
    @Ranadip Dutta I wanted the custom object to work but unfortunately since the whole script itself is,looping, the headers continue to be outputted each iteration :(. The f operator however works lovely: D – Cataster Feb 26 '19 at 16:36