Powershell double loop Username Password


I have a question:

 I need to create DTAP environment thats equal. ( the users are local users special for a FTP site). It are about 1200 usernames.we are using W2012R2 with powershell 4.0.

I have 2 txt files,  username.txt and password.txt

I am having problems with a double loop.

The main problem that my second get content add all the information in one line  so i have a password that has all the information from the full txt file.

Do i have to use: Foreach-object? or is there another solution to create a username and password. that use the content correctly? 

for example :









the lines having a one on one relationship   so : username Ronald has as password FRW@##$$%

Iam doing the following

Import-Module WebAdministration
Function CreateFTPUsers
$users = Get-Content c:\work\username.txt

$password = get-content c:\work\password.txt
 Foreach ($_ in $users)


Foreach ($_P in $password)


#Creates Local users
NET USER "$_" "$password" /ADD
Write-Host "Created user $_ with password $password" 
 $LogDir = "c:\inetpub\logs\ftpsvc\"
$FTPUserDir = "c:\work\ftp\"
$FTPUserPath = ($Username.Split("\")[1])
New-Item IIS:\Sites\FTP\$FTPUserDir\$FTPUserPath -Type VirtualDirectory -PhysicalPath "$LogDir" -force
Add-WebConfiguration -Filter /System.FtpServer/Security/Authorization -Value (@{AccessType="Allow"; Users="$FTPUserPath"; Permissions="Read, Write"}) -PSPath IIS: -Location "FTP/$FTPUserDir/$FTPUserPath"
Clear-WebConfiguration -Filter /System.FtpServer/Security/Authorization -PSPath IIS: -Location "FTP/$FTPUserDir"



0 Comments   [ + ] Show comments

Answers (2)

Posted by: anonymous_9363 5 years ago
Red Belt
Why use separate files?!? Use one file and separate the username and password using a character that will never appear in either, e.g. Tab.

Then you can loop through the content and use Split to separate them into the required strings.

  • To elaborate further on VBSCab.

    $UserNamesAndPasswords = (Import-Csv $DataCSV -Header UserName,Password)[1..($DataCSV.length - 1)]

    The [1..($DataCSV.length - 1)] part removes the header line in the CSV, omit this line if you do not have a header.

    You can use for ForEach loop to cycle through the contents of the array.
    ForEach ($_ in $UserNamesAndPasswords)
    {Do something here
    Write-Host $_.UserName
    Write-Host $_.Password} - rileyz 5 years ago
Posted by: flip1001 5 years ago
Black Belt
Try something such as this code I have posted below

$users = Get-Content "username.txt"
$passwords = Get-Content "password.txt"

# Quit if files are not the same length
if ($users.count -ne $passwords.count) {Exit}

$count = $users.count - 1

0..$count | ForEach-Object {
$user = [string] $users[$_]
$password = [string] $passwords[$_]

Write-Output "User: $user Password: $password"

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:


This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ