For string formatting, you should look at the format operator (-f) and use it to get the width you want for the output.

For example, you can do something like this:"{0,-20}: {1}" -f 'Label','value'In that command, the first string (index 0) will be output into a 20-character wide buffer (that's the number 20), left justified (the minus sign before the 20). If you want a wider label, just increase the number of 20 to something more appropriate for you. If you want the label to be right-justified, you can do that by removing the minus sign.

by suave9987 at 2012-09-17 10:46:41

Great this looks very promissing, however How would i incorporate this in my script? Would i just re write without the -replace cmdlet and then foreach-object insert something along the lines of what you have gave me? I have used this before but it was when I was outputting strings instead of manipulating.

Thanks so much!

by poshoholic at 2012-09-17 11:07:08

It seems from your file that it is space delimited, however some values may also have spaces in them which complicates things a little. I would probably read the content of the file using Get-Content, use a regular expression to separate out the values I want (i.e. something that would identify the first value as everything up to the first space, the last value as a numeric value in brackets, and anything in between as the second value), and then create the desired format either using the format command as I suggested or even easier, simply pipeline the results to Format-Table and then output that to the new text file using a wide format (see this post: http://poshoholic.com/2010/11/11/powershell-quick-tip-creating-wide-tables-with-powershell/). With that approach, you don't even need the format operator, so the challenge becomes parsing the text you get in and converting it into an object that you can then manipulate. Here's an untested, simple example of what that might look like:Get-Content $file.Fullname | ForEach-Object { $value = $_ # This should look something like this: 'A00000000F0002FFF1F5108900000920 NSET Changer applicationVariable (2)' if ($value -match '^(\S+)\s(.+)\s(\(\d+\))?$') { New-Object -TypeName PSObject -Property @{ Id = $matches[1] Value = $matches[2] Count = $(if ($matches.Count -eq 4) {$matches[3]} else {$null}) } }} | Format-Table -Property Id,Value,Count

by suave9987 at 2012-09-17 11:54:16

Wow this works exceptionally well! I will tinker with it a little and make it match my specificiations. I appreciate your help.

I tried using out-file but this will not send any items through the out-file pipe. It actually erases all data.

Any suggestions?

by poshoholic at 2012-09-17 12:08:17

You don't want to use Set-Content before Format-Table. The logic would be the opposite — format the table, then write it to disk. Get-Content to read the content, ForEach-Object to process it, Format-Table with -Property and -AutoSize to get a tabular output with the properties you want, Out-String to convert that to a maximum width per row, and then Out-File to write it to disk. Those last three in sequence are described in that blog post I linked to earlier.

Also, if you're trying to replace the existing content in those files, you might want to read and store the content with Get-Content so that the file is not in use later when you write it, then process it, then write it back to disk.

You should probably also back up your files to make sure you don't accidentally lose anything.

by suave9987 at 2012-09-17 12:17:31

Thanks a ton Kirk. I will read the article and will also try to incoropate all of your ideas here. I appreciate this.