Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Write string with built-in handling of plural-s for variable integer.
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