Updated PowerShell Formatting Functions

Last year I posted an article and a set of PowerShell functions to make it easier to format values. For some reason, I decided to revisit the functions and ended up revising and extending them. I modified Format-Value so that you can format a number as a currency or a number.

Format-Value help (Image Credit: Jeff Hicks)

The function is assuming you will pipe values to it.

Format-Value examples (Image Credit: Jeff Hicks)

I also wrote a new function called Format-String.

Format-String help (Image Credit: Jeff Hicks)

The nice thing is that you can apply as many or all of the formatting transformations as you want.

Format-String examples (Image Credit: Jeff Hicks)

The Replace option works by taking a hashtable and replacing each matching key with its value.

Using the Replace parameter in Format-String (Image Credit: Jeff Hicks)

One task that comes to mind where Format-String might come in handy is in building passwords.

The function contains the following aliases:

  • fp = Format-Percent
  • fv = Format-Value
  • fs = Format-String

I’m also trying something different, at least for me. Instead of posting the code here, I’ve started to use GitHub since that seems to be what all the cool kids are doing. You can find the FormatFunctions module at https://github.com/jdhitsolutions/FormatFunctions. This should also make it easier for me to post revisions, address bugs and make it easier for you to take my code and run with it. You’re still welcome to post comments here, but use GitHub for anything specific to the PowerShell code.



Friday Fun with Formatting

PowerShell is very adept at retrieving all sorts of information from computer systems in your network. Often the data is in a format that is hard to digest at a glance. For example, when you see a value like 1202716672 is that something in MB or GB? What if you need to view that value as KB? I don’t know about you but I’m not good at making those conversions in my head and on-the-fly. Instead, we often resort to commands like this.

That is much easier than seeing a value of 8589934592. The same thing applies when it comes to calculating percentages. Again, we’re back to using custom hashtables with Select-Object. Of course, you can go the extra mile and create custom format and type extensions. But sometimes, at least speaking for myself, you want something in between. So I created two functions, Format-Value and Format-Percent and packaged them as a module.

Format-Value will take any value and by default round it to an integer. Or you can specify to format the value as a certain unit of measurement such as GB or MB. You can even specify the number of decimal places. Or, you can use the -Autodetect parameter and the function will make the best guess about the ‘size’ of your value. Use -Verbose if you want to know what unit it detects.

I did a similar thing with percentages. All you have to do is specify a value and a total and the function does the rest. Again, you can specify the number of decimal places. By default, the function writes a numeric value to the pipeline which I find makes it easier if you want to sort. But you can also specify to get a percentage as a string which will use the -f operator. The string will include the % symbol. I take an extra step to remove the space between the value and symbol.

With the function, my PowerShell expressions become a little easier to write.

In the code sample I’m using the function and parameter aliases. But I like the result.

Download FormatFunctions.zip extract it to your modules directory and you should be good to go. I’ve set the minimum PowerShell version to 3.0 although there’s nothing I can think of that would prevent these commands from working on v2. But I’d like to drive you to at least 3 anyway.

I hope you’ll let me know what you think and what other types of data formatting you’d like to see added to the module.

NOTE: An update has been posted at http://bit.ly/1PzSnJa

Format Leading Zeros in PowerShell

I’ve been working on a question in the forums at ScriptingAnswers.com and the need arose to create a folder name with a 4 digit number. But the number needed to have enough leading zeros so that the number was always 4 digits. For example, Test_0005 or Test_0456. The solution is to use the -f Format operator.

To use this operator on the left side of the statement is a formatting directive and on the right side of -f operator is the value to be formatted. To get the leading zeros I’m going to use a formatting string like this:


This basically says, “take a value and make pad it with 4 0s. Here are some examples:

PS C:\> "{0:0000}" -f 4
PS C:\> "{0:0000}" -f 45
PS C:\> "{0:0000}" -f 456
PS C:\> "{0:0000}" -f 4567

Technically, these are string objects, but that’s ok. Let’s see how we might use this.

PS C:\> 1..10 | foreach {
>> $i="{0:0000}" -f $_
>> $dir="c:\test\Target_$i"
>> $file="file_$i.txt"
>> $target=Join-Path -Path $dir -ChildPath $file
>> Write-Host "Updating $target"
>> }
Updating c:\test\Target_0001\file_0001.txt
Updating c:\test\Target_0002\file_0002.txt
Updating c:\test\Target_0003\file_0003.txt
Updating c:\test\Target_0004\file_0004.txt
Updating c:\test\Target_0005\file_0005.txt
Updating c:\test\Target_0006\file_0006.txt
Updating c:\test\Target_0007\file_0007.txt
Updating c:\test\Target_0008\file_0008.txt
Updating c:\test\Target_0009\file_0009.txt
Updating c:\test\Target_0010\file_0010.txt
PS C:\>

Within the For loop I’m taking the piped in number and formatting to 4 places with leading zeros. This value is then inserted into variables for paths and files, using Join-Path to combine them. This is a much better technique than trying to concatenate.

I hope you find this useful.