Paramater validation - size in MB

I’m writing a function to set values for Exchange (quotas, etc.) - using the set-mailbox cmdlet in a larger script.

Anyone know how to validate a parameter input is a size (like 100MB, 2GB, etc.)?

Use a regular expression:

PS E:\> "100MB" -match '[. 0-9]+(KB|MB|GB)'
True
PS E:\> "99GB" -match '[. 0-9]+(KB|MB|GB)'
True
PS E:\> "987HB" -match '[. 0-9]+(KB|MB|GB)'
False

Credit for the regex goes here: https://www.regextester.com/108417

Perfect! That’s what I needed. Thank you.

        [Parameter()]
            [ValidateScript({
                if($_ -notmatch '[. 0-9]+(KB|MB|GB)') {throw "Invalid value"} 
                return $true
            })]
            [string]$IssueWarningQuota

@Darwin-Reiswig might be good to use `[ValidatePattern()]` here instead:

function Test-Function {
    [CmdletBinding()]
    param(
        [Parameter()]
        [ValidatePattern('[. 0-9]+(KB|MB|GB)')]
        [string]
        $IssueWarningQuota
    )
<# ... #>
}

Slick. I’ve never used validatepattern before. Learned something new today :slight_smile:

Thanks!