external help file | Module Name | online version | schema |
---|---|---|---|
PSScriptTools-help.xml |
PSScriptTools |
2.0.0 |
Format a value as a percentage.
Format-Percent [-Value] <Object> [-Total] <Object> [-Decimal <Int32>]
[<CommonParameters>]
Format-Percent [-Value] <Object> [-Total] <Object> [-Decimal <Int32>]
[-AsString] [<CommonParameters>]
This command calculates a percentage of a value from a total, with the formula (value/total)*100. The default is to return a value to 2 decimal places but you can configure that with -Decimal. There is also an option to format the percentage as a string which will include the % symbol.
PS C:\> Format-Percent -value 1234.567 -total 5000 -decimal 4
24.6913
Calculate a percentage from 1234.567 out of 5000 (i.e. 1234.567/5000) to 4 decimal points.
PS C:\> Get-CimInstance win32_operatingsystem -computer chi-dc04 |
Select-Object PSComputername,TotalVisibleMemorySize,
@{Name="PctFreeMem";Expression={
Format-Percent $_.FreePhysicalMemory $_.TotalVisibleMemorySize}}
PSComputerName TotalVisibleMemorySize PctFreeMem
-------------- ---------------------- ----------
chi-dc04 1738292 23.92
PS C:\> Get-CimInstance win32_operatingsystem -computer chi-dc04 |
Select-Object PSComputername,TotalVisibleMemorySize,
@{Name="PctFreeMem";Expression={
Format-Percent $_.FreePhysicalMemory $_.TotalVisibleMemorySize -asString}}
PSComputerName TotalVisibleMemorySize PctFreeMem
-------------- ---------------------- ----------
chi-dc04 1738292 23.92%
The numerator value.
Type: Object
Parameter Sets: (All)
Aliases: X, Numerator
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The denominator value.
Type: Object
Parameter Sets: (All)
Aliases: Y, Denominator
Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The number of decimal places to return between 0 and 15.
Type: Int32
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: 2
Accept pipeline input: False
Accept wildcard characters: False
Write the result as a string.
Type: SwitchParameter
Parameter Sets: String
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/