John Ring
John Ring

Reputation: 57

PowerShell Error - Method Not Found

When using $Computer.StartsWith("WI-") I get the following error

   Method invocation failed because [Microsoft.ActiveDirectory.Management.ADComputer] does not contain a method named 'StartsWith'

I am under the impression that this is a default method. Is there something I have to import to use this?

Upvotes: 0

Views: 2166

Answers (2)

JosefZ
JosefZ

Reputation: 30123

That error is pretty clear: an object of [Microsoft.ActiveDirectory.Management.ADComputer] type does not contain a method named 'StartsWith'.

Where the $Computer comes from? From Get-ADComputer cmdlet? Read How to list all AD computer object properties

Running $Computer | Get-Member | ft -AutoSize should prompt more.

Run $Computer.GetType() as well. For instance, next could work if $Computer is not an array:

$Computer.Name.StartsWith("WI-")
$Computer.CN.StartsWith("WI-")
$Computer.DisplayName.StartsWith("WI-")

However, next similar expressions could give another results:

$Computer.Name.ToUpper().StartsWith("WI-")
$Computer.CN.ToUpper().StartsWith("WI-")
$Computer.DisplayName.ToUpper().StartsWith("WI-")

Upvotes: 0

Jan Skála
Jan Skála

Reputation: 415

Try this instead

$env:COMPUTERNAME.StartsWith("WI-")

Upvotes: 1

Related Questions