Powershell wildcard problem
-
Friday, February 17, 2012 1:30 PM
(get-content c:\file.txt) | Foreach-Object {$_ -replace "cn=", ""} |
Foreach-Object {$_ -replace ",*", ""}contents of file.txt:
CN=COMPUTERNAME,OU=ITOU,DC=domain,DC=com
CN=COMPUTERNAME2,OU=DIFFRENTOU,DC=domain,DC=comThe above is the code i run as a powershell script. and the goal is to take away cn= and , and everything behind ,
So naturally i just replaced "cn=" with "" and ",*" with "". This, does not work as intended.what i am left with is:
COMPUTERNAMEOU=ITOUDC=domainDC=com
COMPUTERNAME2OU=DIFFRENTOUDC=domainDC=comHow could i achieve my goal of only being left with:
COMPUTERNAME
COMPUTERNAME2please provide an explaination along with your suggested changes! Would be much obliged!
All Replies
-
Friday, February 17, 2012 1:36 PM
I believe a dot is required for the replace command you are attempting. Could you try this:
-replace ",ou.*",""
-
Friday, February 17, 2012 1:38 PM
Just for to be complete I have added into your example
(get-content c:\file.txt) | Foreach-Object {$_ -replace "cn=", ""} | Foreach-Object {$_ -replace ",.*", ""}
- Marked As Answer by Bartek BielawskiModerator Friday, February 17, 2012 2:02 PM
-
Friday, February 17, 2012 1:39 PM
Or even easier:
(get-content c:\scripts\file.txt) | Foreach-Object {($_ -split ',')[0] -replace 'CN='}
Grant Ward, a.k.a. Bigteddy
What's new in Powershell 3.0 (Technet Wiki)
- Edited by BigteddyMicrosoft Community Contributor Friday, February 17, 2012 1:44 PM
- Marked As Answer by Bartek BielawskiModerator Friday, February 17, 2012 2:02 PM
-
Friday, February 17, 2012 2:11 PMModerator
Or:(get-content c:\scripts\file.txt) -replace '^cn=(.+?),(ou|dc)=.+$','$1'
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
-
Friday, February 17, 2012 2:27 PM
Thanks alot you guys.
Seems like all you do is wait for noobs to impress with your scripting knowledge!
- Proposed As Answer by BigteddyMicrosoft Community Contributor Friday, February 17, 2012 2:29 PM
- Unproposed As Answer by BigteddyMicrosoft Community Contributor Friday, February 17, 2012 2:29 PM
-
Friday, February 17, 2012 2:34 PMNa, it's just mj with his latest Regex magic trick. It's just not poplular with the masses, Rob.
Grant Ward, a.k.a. Bigteddy
-
Friday, February 17, 2012 2:35 PMModerator
We all have our favorite ways of doing things, I think it's good for the 'noobs' to get exposed to different ways of solving a problem. Then they have more ideas about what their options are for solving similar problems later.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
-
Friday, February 17, 2012 4:45 PMModerator
It's just not poplular with the masses, Rob.
Give me time........
Grant Ward, a.k.a. Bigteddy
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

