Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create printf effect in PowerShell

How can I get my PowerShell script to print the info in tabular format as the script progresses.

In bash I would do this by

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 get

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

like image 881
rojanu Avatar asked Mar 28 '12 09:03

rojanu


People also ask

What does @() mean in PowerShell?

Array subexpression operator @( )Returns the result of one or more statements as an array. The result is always an array of 0 or more objects. PowerShell Copy.

How do you print text in PowerShell?

The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output.

How do I create a loop in PowerShell?

The While statement in PowerShell is used to create a loop that runs a command or a set of commands if the condition evaluates to true. It checks the condition before executing the script block. As long as the condition is true, PowerShell will execute the script block until the condition results in false.


2 Answers

Try this out in your PowerShell console:

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

You can use string formatting quite easily from PowerShell.

The -f operator is a PowerShell short cut to the String.Format function, including all the standard and custom formatting .NET types support.

like image 157
David Brabant Avatar answered Sep 24 '22 09:09

David Brabant


I have accepted Davids answer as that's what I asked for. However, I have chosen to create an object by

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 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 the code do

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

to print the entries

like image 45
rojanu Avatar answered Sep 21 '22 09:09

rojanu