Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If strings starts with in PowerShell [duplicate]

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     } } 
like image 745
JocSch Avatar asked Feb 26 '16 14:02

JocSch


People also ask

What does %% mean in PowerShell?

% is an alias for the ForEach-Object cmdlet. An alias is just another name by which you can reference a cmdlet or function.

How do I match a string in PowerShell?

If you want to find the string at certain positions within the string, use ^ to indicate the beginning of the string and $ to indicate the end of the string. To match the entire string, use both. Note the ^ at the start and the $ at the end to ensure that the entire input string must match.

How do you write an IF THEN statement in PowerShell?

The syntax of If statements in PowerShell is pretty basic and resembles other coding languages. We start by declaring our If statement followed by the condition wrapped in parentheses. Next, we add the statement or command we want to run if the condition is true and wrap it in curly brackets.

What does .split do in PowerShell?

The Split operator splits one or more strings into substrings. You can change the following elements of the Split operation: Delimiter. The default is whitespace, but you can specify characters, strings, patterns, or script blocks that specify the delimiter.


1 Answers

$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_").

like image 95
M.G Avatar answered Oct 03 '22 13:10

M.G