0

I want to search and replace parts of UserObject paths using regex.

If you query windows for Users in Local Groups it returns the members as in the example below. Local users are displayed with an domain related prefix and i want to find this domain prefix an delete it form local user paths.

Return Value:

\\MyDomain\PCTest\John Doe   #(Local User)
\\MyDomain\Julie Doe         #(Domain User)

After Formating: (how can i do this?)

\\PCTest\John Doe             #(Local User)
\\MyDomain\Julie Doe          #(Domain User)

4 Answers 4

2

This will remove the first element if the path contains more than two elements:

'\\MyDomain\PCTest\John Doe','\\MyDomain\Julie Doe' | Foreach-Object{

    if( ($items = $_.Split('\',[System.StringSplitOptions]::RemoveEmptyEntries)).Count -gt 2)
    {
        '\\'+ ($items[1..$items.count] -join '\')

    }
    else
    {
        $_
    }
}

\\PCTest\John Doe
\\MyDomain\Julie Doe
Sign up to request clarification or add additional context in comments.

Comments

0

supposing your values are stored in c:\temp\users.txt :

gc C:\temp\users.txt |%{if ($_ -match "local"){$_.replace("MyDomain\","")}}

edit :

$slices="\\MyDomain\PCTest\John Doe".split('\')
if($slices.Count -eq 5){wh "\\$($slices[3])\$($slices[4])"}

1 Comment

Hi, Local and Domain are comments they're not appering in the file/object. My start value is an array with mixed entries like in the Example RETURN VLAUE obove. In this arry are multiple entries for domain and local users. I only want to Trim/Replace the Domain referenz form the local user like "John Doe" above.
0
"\\MyDomain\PCTest\John Doe", "\\MyDomain\Julie Doe" | % {
  $_ -replace '\\MyDomain(\\.*\\)', '$1'
}

Comments

0

Another possibility:

'\\MyDomain\PCTest\John Doe','\\MyDomain\Julie Doe' |
ForEach-Object {
'\\{0}\{1}' -f $_.split('\')[-2,-1]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.