How to create a printf effect in PowerShell

How can I get the PowerShell script to print information in a tabular format as the script progresses.

In bash, I would do this with

printf "%s\t%-15.15s" "Locale" "Jar" if($verbose);then printf "%-15.15s %-15.15s" "HelpSet" "Exception" fi printf "\t%s\n" "Status" ... printf "%s\t%-15.15s" $locale $helpFileName if($verbose); then printf "%-15.15s %-15.15s" "$helpSetName" ${exclusion[$helpFileName]} fi status="OK" ... if ($fixed); then status="CORRECTED" fi printf "\t%s\n" $status 

To obtain

 Locale Jar HelpSet Exception Status de help_D150 help_D150 CORRECTED es help_D150 help_D150 OK fr help_D150 help_D150 OK it Locale folder not found nl help_D150 help_D150 CORRECTED 

thanks

+8
powershell printf
source share
2 answers

Try this in the PowerShell console:

 "{0}`t{1,-15}{2,-15}{3,-15}" -f "Locale", "Jar", "HelpSet", "Exception" 

You can easily use string formatting from PowerShell.

The -f operator is an abbreviation of PowerShell to String.Format , including all standard and custom .NET support formats.

+16
source share

I accepted Davids' answer as what I requested. However, I decided to create an object

 try{ add-type @' namespace FFPS { public class Data { public string Locale; public string JarFile; public string HelpSet; public string CorrectName; public string Status; } } '@ } catch{} 

and then use the XML format file to format it as a table

 <?xml version="1.0" encoding="utf-16"?> <Configuration> <ViewDefinitions> <View> <Name>ffps.data</Name> <ViewSelectedBy> <TypeName>ffps.data</TypeName> </ViewSelectedBy> <TableControl> <TableHeaders> <TableColumnHeader> <Label>Locale</Label> <Width>6</Width> </TableColumnHeader> <TableColumnHeader> <Label>Jar File</Label> <Width>16</Width> </TableColumnHeader> <TableColumnHeader> <Label>Help Set</Label> <Width>16</Width> </TableColumnHeader> <TableColumnHeader> <Label>Correct Name</Label> <Width>16</Width> </TableColumnHeader> <TableColumnHeader> <Label>Status</Label> <Width>100</Width> </TableColumnHeader> </TableHeaders> <TableRowEntries> <TableRowEntry> <TableColumnItems> <TableColumnItem> <ScriptBlock>$_.Locale</ScriptBlock> </TableColumnItem> <TableColumnItem> <ScriptBlock>$_.JarFile</ScriptBlock> </TableColumnItem> <TableColumnItem> <ScriptBlock>$_.HelpSet</ScriptBlock> </TableColumnItem> <TableColumnItem> <ScriptBlock>$_.CorrectName</ScriptBlock> </TableColumnItem> <TableColumnItem> <ScriptBlock>$_.Status</ScriptBlock> </TableColumnItem> </TableColumnItems> </TableRowEntry> </TableRowEntries> </TableControl> </View> </ViewDefinitions> </Configuration> 

and in do code

 $currentFile = New-Object ffps.data $currentFile.Locale = "DE" $currentFile.JarFile = "JarFile.Name" ... $currentFile 

for printing records

+2
source share

All Articles