0

Let say I have text file contains the following strings.

user1
user2
user3
user4

How do I pass all those data to another template file contains the following ..

UserCreate 'string here' /GROUP:none /REALNAME:none /NOTE:none
UserNTLMSet 'string here' /ALIAS:none

So at the end it will produce output like this

UserCreate user1 /GROUP:none /REALNAME:none /NOTE:none
UserNTLMSet user1 /ALIAS:none
UserCreate user2 /GROUP:none /REALNAME:none /NOTE:none
UserNTLMSet user2 /ALIAS:none
.....................

Thank you in advance!!

2 Answers 2

1

Really easy:

# create the template as Here-String
$template = @"
UserCreate {0} /GROUP:none /REALNAME:none /NOTE:none
UserNTLMSet {0} /ALIAS:none
"@

# read the text file as string array
Get-Content -Path 'D:\TheUsersFile.txt' | ForEach-Object {
    $template -f $_
}

Output:

UserCreate user1 /GROUP:none /REALNAME:none /NOTE:none
UserNTLMSet user1 /ALIAS:none
UserCreate user2 /GROUP:none /REALNAME:none /NOTE:none
UserNTLMSet user2 /ALIAS:none
UserCreate user3 /GROUP:none /REALNAME:none /NOTE:none
UserNTLMSet user3 /ALIAS:none
UserCreate user4 /GROUP:none /REALNAME:none /NOTE:none
UserNTLMSet user4 /ALIAS:none

The -f Format operator replaces all numbered placeholders in a string like {0} with the value(s) on the right-hand side. You can have more than one placehlder of course. For instance

"This {0} gets filled in with your name: {1}" -f 'string', $env:USERNAME
Sign up to request clarification or add additional context in comments.

Comments

1

You may try the following:

foreach ($user in (Get-Content -Path C:\TEMP\users.txt)){
    UserCreate 'string here' /GROUP:none /REALNAME:none /NOTE:none
    UserNTLMSet 'string here' /ALIAS:none
}

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.