Author Posts

December 7, 2017 at 12:52 pm

Hi

I have a .csv file with below content.

XXXGRP1001 – XXX-AD-groupname
XXXGRP1002 – XXX-AD-groupname
XXXGRP1003 – XXX-AD-groupname
XXXGRP1004 – XXX-AD-groupname
XXXGRP1005 – XXX-AD-groupname

I would like to replace "-" at 10th character with "," and split in to columns in three columns A(first 10 characters) and B(remaining characters) and Third column C will have AD-group users.
Could you please let me know how to achieve this task. I am new to Powershell.

Thanks
Raghav

December 7, 2017 at 4:57 pm

Is it a csv file or is it a text file? You could read the content of a text file with Get-Content and treat every line as needed or you could import your csv file with Import-CSV and treat the specific column as needed.

You can use regular expressions to cut your string in pieces as needed or you could use substring method to do so.

December 7, 2017 at 6:24 pm

Example using just your string snippet...

$StringToSplit = @'
XXXGRP1001 – XXX-AD-groupname
XXXGRP1002 – XXX-AD-groupname
XXXGRP1003 – XXX-AD-groupname
XXXGRP1004 – XXX-AD-groupname
XXXGRP1005 – XXX-AD-groupname
'@

$StringToSplit -replace ' – ',',' -replace 'AD-','AD,'

Results

XXXGRP1001,XXX-AD,groupname
XXXGRP1002,XXX-AD,groupname
XXXGRP1003,XXX-AD,groupname
XXXGRP1004,XXX-AD,groupname
XXXGRP1005,XXX-AD,groupname