Home > OS >  Powershell splitting line from output
Powershell splitting line from output

Time:03-30

I dabbled here and there with PowerShell but I still need some work. I am trying to take an output from a line of a text file to get ready to use it to migrate to another domain with same user name base. The extract is a group name called "Group-Executives-ronly" and the next set of data is the groups and users in that group called "Group-Executives-ronly"

$variable1 = '"Group-Executives-ronly","Group Approvers for accounts;Wilson, Frank fwilson;Jorden, mike mjorden;smith, David dsmith"'

I am just displaying result before I actually use this data to do the other work. I would like to get each line printed like so but every iteration of split is not working well for the user name section.

Group-Executives-ronly

Group Approvers for accounts
fwilson
mjorden
dsmith

I had first broken it up with

$group=$variable1.Split('"')\[1\]
$Userslist=$variable1.Split('"')\[3\]

Which gave the first array the Group-Executives-ronly and the 3rd array Group Approvers for accounts;Wilson, Frank fwilson;Jorden, mike mjorden;smith, David dsmith

I did another array for splitting with ;

$Users=$Userslist.Split(';')

and that gave me all the group/users split like so

Group Approvers for accounts

Wilson, Frank fwilson

Jorden, mike mjorden

smith, David dsmith

I like that I got the full group name but I don't want the name of those users, I just need their UID, is that possible, am I going at it wrong? The desired output needed is

Group Approvers for accounts

fwilson

mjorden

dsmith

CodePudding user response:

What Santiago Squarzon said.

You're close: from the userlist after splitting in ';', if it contains a ',' then split on space and grab the last item, otherwise just take the whole userlist item.

PS /> $variable1 = '"Group-Executives-ronly","Group Approvers for accounts;Wilson, Frank fwilson;Jorden, mike mjorden;smith, David dsmith"'
PS /> $group=$variable1.Split('"')[1]                                                                                                      
PS /> $Userslist=$variable1.Split('"')[3]                                                                                                  
PS /> $Userslist.split(';') | %{if ($_.contains(',')){$_.split(' ')[-1]} else {$_}}                                                        
Group Approvers for accounts
fwilson
mjorden
dsmith
PS /> 
  • Related