If strings starts with in PowerShell [duplicate]
Asked Answered
J

1

64

Is there a way to check if a string starts with a string?

We are checking the groupmembership from the AD user. Our AD groups look like this: S_G_share1_W

The script for connecting the networkshares should only run if the groupname starts with "S_G_", because we have some other groups too.

$GroupArray = Get-ADPrincipalGroupMembership $env:USERNAME | select samaccountname

foreach ($Group in $GroupArray) {

    if ($Group.StartsWith("S_G_")) {

        $Group = $Group -replace "S_G_", $FileServerRV
        Write-Host $Group

        $Group = $Group.Substring(0, $Group.Length-2)
        Write-Host $Group

        #erstellen des Anzeigennames
        $Groupname = $Group.Replace($FileServerRV, "")
        Write-Host "Call Function with parameter "$Group $Groupname
    }
}
Japan answered 26/2, 2016 at 14:40 Comment(3)
$Group.StartsWtih("string")Idiopathy
Answer shows how to use startswith which is what you are asking for.Idiopathy
It's wright, that your answer provides a link to "How to use the StartsWith() function", but the problem which @Japan has in this case is, that he tries to use the StartsWith() function on the object $Group and not on the acutal property of this object which is $Group.samaccountnameWine
W
94

$Group is an object, but you will actually need to check if $Group.samaccountname.StartsWith("string").

Change $Group.StartsWith("S_G_") to $Group.samaccountname.StartsWith("S_G_").

Wine answered 26/2, 2016 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.