Posted by: R Manimaran | July 16, 2012

PowerShell File output Formatting

Formatting in PowerShell using Formatters

Powershell has the feature to write the information to a file. I used the PowerShell to do the initial setup like creating list, libraries, site columns, content types and so on. While doing I used to log the information in a log file for future reference.

I used the following statement in the Powershell to do so.

$File =”. InitialSetup.log”

“Creating List and Libraries” |Out-File $File – append

The above statements in PowerShell will create a file named InitialSetup.log under the current PowerShell running directory.

While writing the output to the Log file, I used the tab switch to format somewhat like below.

The switch used to create the Tab space is in Powershell is `t (Press the Key below Esc and press t)

Tab – `t

New Line – `n

Code is as below

But I feel the alignment of the columns are not good. See the Status and Created in the above log file. When I searched more I found another switch called format switch(`f).

When I apply this my output lines something like this..

Looks good know. And the code for getting this format is

How this format works. Simple, above you see {0,-40}{1,-20}.

Here 0 and 1 is the no. of arguments you are going to have. We have only two (i..e Library Name & Status)

40 and 20 represents the two character space need to print the message. Here Library Name I set to have 40 characters space and status to have 20 character space.

Then what is the –(minus) represents. It force the output to print in Left intent in the give space. Positive will print the message in Right intent.

Here is the output for positive numbers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: