Solved Batch script ro read csv file and output text to file

May 25, 2012 at 04:06:39
Specs: Windows 7
Hi

Im hoping someone can help me with a batch file, I need a script to read a simple csv file that contains email addresses

emailaddress
bob@company.com
john@company.com
mary@company.com
etc...

then i need for every email address thats read for the script to output to a txt file the below with the "NewPassword101" number incremented by +1 every output

Set-MsolUser -UserPrincipalName bob@company.com -StrongPasswordRequired $false
Set-MsolUserPassword bob@company.com -NewPassword "NewPassword101"
Set-MsolUser -UserPrincipalName bob@company.com -PasswordNeverExpires $true

so the out put file should look like the below?

txt file------------------------------------

Set-MsolUser -UserPrincipalName bob@company.com -StrongPasswordRequired $false
Set-MsolUserPassword bob@company.com -NewPassword "NewPassword101"
Set-MsolUser -UserPrincipalName bob@company.com -PasswordNeverExpires $true

Set-MsolUser -UserPrincipalName john@company.com -StrongPasswordRequired $false
Set-MsolUserPassword john@company.com -NewPassword "NewPassword102"
Set-MsolUser -UserPrincipalName john@company.com -PasswordNeverExpires $true

Set-MsolUser -UserPrincipalName mary@company.com -StrongPasswordRequired $false
Set-MsolUserPassword mary@company.com -NewPassword "NewPassword103"
Set-MsolUser -UserPrincipalName mary@company.com -PasswordNeverExpires $true

end of txt file-------------------------------------------------------

Can anyone help me with this ive tried but im no coder :)


See More: Batch script ro read csv file and output text to file

Report •


#1
May 25, 2012 at 05:53:04
✔ Best Answer
If your .csv file just holds one column as posted, here the script

@echo off & setlocal EnableDelayedExpansion
set cnt=100
for /F "delims=" %%j in ('type "Input.csv"') do (
  set /A cnt+=1
  echo.Set-MsolUser -UserPrincipalName %%j -StrongPasswordRequired $false
  echo.Set-MsolUserPassword %%j -NewPassword "NewPassword!cnt!"
  echo.Set-MsolUser -UserPrincipalName %%j -PasswordNeverExpires $true
) >> "Output.txt"


Report •

#2
May 25, 2012 at 06:03:57
It doesnt work for me?

I have created Input.csv with one column

I dont get any output.txt created in the root folder?


Report •

#3
May 25, 2012 at 06:10:41
ah if i run it from a cmd prompt it works thanks man!

Report •

Related Solutions

#4
May 25, 2012 at 06:19:33
Thanks you have just saved me hours of editing thanks a million!!!

Report •

#5
May 25, 2012 at 07:18:51
I should point out that you're using CMD to write PowerShell commands. This might be a sign you're doing something wrong.
Get-Content input.csv | ForEach-Object {
  Set-MsolUser -UserPrincipalName $_ -StrongPasswordRequired $false
  Set-MsolUserPassword $_ -NewPassword ("NewPassword{0:000}" -f ++$cnt)
  Set-MsolUser -UserPrincipalName $_ -PasswordNeverExpires $true
}

How To Ask Questions The Smart Way


Report •

#6
May 26, 2012 at 08:16:45
"ah if i run it from a cmd prompt it works thanks man!"

gee, who'da thought


=====================================
Life is too important to be taken seriously.

M2


Report •


Ask Question