Created
October 8, 2014 13:39
-
-
Save gravejester/1c9e354ff726d6d78ae2 to your computer and use it in GitHub Desktop.
Write string with built-in handling of plural-s for variable integer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-PluralString{ | |
<# | |
.SYNOPSIS | |
Write string with built-in handling of plural-s for variable integer. | |
.DESCRIPTION | |
Write string with built-in handling of plural-s for variable integer. | |
.EXAMPLE | |
Write-PluralString "$($computer): " $num 'file' 'processed' | |
.EXAMPLE | |
Write-Verbose (Write-PluralString "$($computer): " $num 'file' 'processed') | |
.EXAMPLE | |
Write-PluralString '' $num 'file' $processed | |
Omitting the PreString | |
.NOTES | |
Name: Write-PluralString | |
Author: Øyvind Kallstad | |
Created: 18.08.2014 | |
Version: 1.0 | |
#> | |
[CmdletBinding()] | |
param ( | |
# Define a starting string. | |
[Parameter(Position = 0, Mandatory = $false)] | |
[string]$PreString, | |
# Integer that will be used as part of the string, and will decide if the StringPart should include the plural-s or not. | |
[Parameter(Position = 1, Mandatory = $true)] | |
[int]$IntPart, | |
# String that will display with or without 's' based on value of the integer before it | |
[Parameter(Position = 2, Mandatory = $true)] | |
[ValidateNotNullorEmpty()] | |
[string]$StringPart, | |
# Define a string to display at the end. | |
[Parameter(Position = 3, Mandatory = $false)] | |
[string]$PostString | |
) | |
Write-Output "$(@{$false="$($PreString) "}[[System.String]::IsNullOrWhiteSpace($PreString)])$($IntPart) $(@{$true = $StringPart; $false = "$($StringPart)s"}[$IntPart -eq 1]) $($PostString)" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment