We help IT Professionals succeed at work.

Upper Case in powershell Paramter

Kelly Garcia
Kelly Garcia asked
on
972 Views
Last Modified: 2017-05-09
HI All,

I have this script, I want the information entered in $VMname variable to be capitalised. what is the best way of doing this?

function CreateVM()
	{
	
	[CmdletBinding()]
          Param(
		  
              [Parameter(Mandatory=$True)]
			  [ValidateSet("ENV-MOB", "ENV-WEB", "ENV-WRK")]
              [string] $VMName
          		)

}

Open in new window



thank you in advance.
Comment
Watch Question

Jason CrawfordTransport Ninja
CERTIFIED EXPERT

Commented:
$VMName.ToUpper()

Open in new window

PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Apologies, typo in my answer. Neglected the parameter name with ValidatePattern.
param (
    [ValidatePattern('^[A-Z]+$', Options = 'None')]
    [String]$VMName
)

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Either add , IgnoreCase=$false to your ValidateSet to enforce it during the call:
	[ValidateSet("ENV-MOB", "ENV-WEB", "ENV-WRK", IgnoreCase=$false)]

Open in new window

Or sometime later in your script, just use
$VMName = $VMName.ToUpper()

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
thank you guys, I don't want to enforce during call just want to convert it :) but it is fantastic information as it's helping me to learn more powershell! People are already starting to call me powershell babe in my company lol
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.