Search code examples
powershellstartswith

I can't get Powershell StartsWith function to work


I'm trying to create a Powershell script that prints out only certain AD groups from the Folder Permission settings. However for some reason Powershell doesn't recognize StartsWith function.

("C:\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).StartsWith("sl_test")) { continue }; $_ }

When I run this I got errors similar to this for every foreach object:

Method invocation failed because [System.Security.Principal.NTAccount] does not contain a method named 'StartsWith'. At C:\temp\test.ps1:1 char:56 + ("C:\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).St ... + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

Any suggestions on how to get this to work?


Solution

  • If you want the string representation of an IdentityReference (regardless of whether it's and NTAccount object or a SID), you can reference the Value property:

    $_.IdentityReference.Value.StartsWith('sl_test')