[help] allow only alphanumeric chars

I

Irwin

$Global:ComputerName = (Get-WmiObject win32_computerSystem).Caption

$NewComputerName = read-host "Current computer name is `"$ComputerName`".
Enter the new name (or leave blank to cancel)"

if ($NewComputerName -NE "") {
$ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem
$ComputerInfo.rename($NewComputerName)
}

I have this code above. Need to allow only alphanumeric characters (and
maybe hyphen -) to $NewComputerName. How do I do it?

Many thanks!
 

My Computer

R

Robert Robelo

$prompt = "Current computer name is `"$ComputerName`". " +
"Enter the new name (or leave blank to cancel)"
do {
$NewComputerName = Read-Host $prompt
} until ($NewComputerName -match '^(\w|-)*$')

--
Robert
 

My Computer

I

Irwin

Thanks, it works!

"Robert Robelo" wrote:

> $prompt = "Current computer name is `"$ComputerName`". " +
> "Enter the new name (or leave blank to cancel)"
> do {
> $NewComputerName = Read-Host $prompt
> } until ($NewComputerName -match '^(\w|-)*$')
>
> --
> Robert
 

My Computer

C

Chris Dent

Just a quick note on \w.

\w will also match underscore (_), it's considered to be a word
character. If you really only want characters then you might be better
served with a character class:

$NewComputerName -Match '^[\da-z\-]*$'

\d for numeric digits, a-z captures both a-z and A-Z (-Match vs
-CMatch), and \- matches hyphen which has to be escaped in the class
with \ because it defines a range otherwise.

Considering you're setting a computer name you might also check maximum
and minimum lengths (between 3 and 20 characters in this example):

$NewComputerName -Match '^[\da-z\-]{3,20}$'

You may also want to check for entirely numeric names, they're no fun.
-Match '^\d*$' is nice and short for that.

Cheers,

Chris


Irwin wrote:

> Thanks, it works!
>
> "Robert Robelo" wrote:
>
>

>> $prompt = "Current computer name is `"$ComputerName`". " +
>> "Enter the new name (or leave blank to cancel)"
>> do {
>> $NewComputerName = Read-Host $prompt
>> } until ($NewComputerName -match '^(\w|-)*$')
>>
>> --
>> Robert
>>
 

My Computer

C

Chris Dent

I forgot to add, the range I set (3 to 20) is entirely arbitrary, any
range you set should be fit for your purposes.

Cheers,

Chris

Chris Dent wrote:

>
> Just a quick note on \w.
>
> \w will also match underscore (_), it's considered to be a word
> character. If you really only want characters then you might be better
> served with a character class:
>
> $NewComputerName -Match '^[\da-z\-]*$'
>
> \d for numeric digits, a-z captures both a-z and A-Z (-Match vs
> -CMatch), and \- matches hyphen which has to be escaped in the class
> with \ because it defines a range otherwise.
>
> Considering you're setting a computer name you might also check
> maximum and minimum lengths (between 3 and 20 characters in this
> example):
>
> $NewComputerName -Match '^[\da-z\-]{3,20}$'
>
> You may also want to check for entirely numeric names, they're no fun.
> -Match '^\d*$' is nice and short for that.
>
> Cheers,
>
> Chris
>
>
> Irwin wrote:

>> Thanks, it works!
>>
>> "Robert Robelo" wrote:
>>
>>

>>> $prompt = "Current computer name is `"$ComputerName`". " +
>>> "Enter the new name (or leave blank to cancel)"
>>> do {
>>> $NewComputerName = Read-Host $prompt
>>> } until ($NewComputerName -match '^(\w|-)*$')
>>>
>>> --
>>> Robert
>>>
 

My Computer

I

Irwin

Thank you for making it perfect Chris Dent

"Chris Dent" wrote:

>
> I forgot to add, the range I set (3 to 20) is entirely arbitrary, any
> range you set should be fit for your purposes.
>
> Cheers,
>
> Chris
>
> Chris Dent wrote:

> >
> > Just a quick note on \w.
> >
> > \w will also match underscore (_), it's considered to be a word
> > character. If you really only want characters then you might be better
> > served with a character class:
> >
> > $NewComputerName -Match '^[\da-z\-]*$'
> >
> > \d for numeric digits, a-z captures both a-z and A-Z (-Match vs
> > -CMatch), and \- matches hyphen which has to be escaped in the class
> > with \ because it defines a range otherwise.
> >
> > Considering you're setting a computer name you might also check
> > maximum and minimum lengths (between 3 and 20 characters in this
> > example):
> >
> > $NewComputerName -Match '^[\da-z\-]{3,20}$'
> >
> > You may also want to check for entirely numeric names, they're no fun.
> > -Match '^\d*$' is nice and short for that.
> >
> > Cheers,
> >
> > Chris
> >
> >
> > Irwin wrote:

> >> Thanks, it works!
> >>
> >> "Robert Robelo" wrote:
> >>
> >>
> >>> $prompt = "Current computer name is `"$ComputerName`". " +
> >>> "Enter the new name (or leave blank to cancel)"
> >>> do {
> >>> $NewComputerName = Read-Host $prompt
> >>> } until ($NewComputerName -match '^(\w|-)*$')
> >>>
> >>> --
> >>> Robert
> >>>
> .
>
 

My Computer

Top