powershell - How to read CSV file based on first letter -


i have csv file list of usernames "jsmith, mdoe, smae, etc."

i need read file in powershell , add them different ad groups based on first letter. "jsmith" need go in group j, "mdoe" need go in group m, , "smae" need go in group s.

how can read data , separate based on first letter?

read file, add group based on .substring(0,1).

if have header line want add select -skip 1 | before %{ loop.

gc file.csv | %{add-adgroupmember ($_.substring(0,1)) $_} 

edit: ok, want broken down i'll best.

gc file.csv - gc short get-content. read file, , pass contents along pipe 1 line @ time.

%{ - short foreach{, things in script block {...} performed each line.

add-adgroupmember ($_.substring(0,1)) $_ - command adds user account ad group. first argument ($_.substring(0,1)) group adding to. reads $_ (the current line being processed) .substring(0,1) means taking part of string, starting @ character 0 (the first character), , grabbing 1 character, (0,1) part of it. , second argument $_ entire line being processed, should user name according said. } - closes foreach loop.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -