Create a txt file per user


In my new-user script, I use a join-path command to create a txt file with the account credentials.
Until now I have not been able to realise this for multiple accounts.

When I have only 1 useraccount to create it all works wel, but when I need to create 2 or more, the txt file per users is not a succes so far.

Create a legacy Service Account

#set every Variable back to $Null

$sa = $Null
$DC = $Null
$Domain = $Null
$ReferID = $Null
$Reference = $Null
$path = $Null
$Description = $Null
$Passw = $Null
$NewSA = $Null

#import the information from a csv 
$sa = import-csv ' X:\Pieter\Scripts\CSV\SA Account.csv' -Delimiter ";" 

$Description = foreach($entry in $sa){ 
    $entry.motivatie + " " + "|" + $entry.wnr

#Create a new useraccount
 $newsa = foreach($account in $sa){
    $Data = @{
        Samaccountname = $account.sanaam
        Userprincipalname =  $account.sanaam  + "@" +$account.domein
        Name = $account.sanaam
        Givenname = $account.sanaam
        Displayname = $
        Server = $Account.domein
        AccountPassword = (ConvertTo-SecureString -AsPlainText $ -Force)
        PasswordNeverExpires = $true
        ChangePasswordAtLogon = $false
        CannotChangePassword = $false
        Enabled =$true
        Path =$account.path
        Description = $account.motivatie + " " + "|" + $account.wnr
        Passthru = $true
        Whatif = $false

    #Create the new user
    write-host 'creating user : '  $account.sanaam -ForegroundColor Green 

    #Eventueel aanpassen ($user = new-aduser @data)
        $user = new-aduser @Data

    #Add the user to Adgroups
    write-host 'Add useraccount : ' $account.sanaam -ForegroundColor Green 'to adgroup' `n $account.Adgroup

    Foreach ($group in $sa){
    $ADG = $group.adgroup -split(',')
         foreach ($Item in $ADG)
         Add-ADGroupMember $Item.trim() -Server $group.domein -Members $group.Sanaam 

     #split the variable to keep just the samaccountname of the ADgroup

$Sam = foreach ($acc in $NewSA){
     ($acc -split "," | select -First 1).trim()

#Create a textfile with credentials ( fr a single account)

$newpath1  = foreach($file in $account){
join-path -Path "T:\Naar CW\"  -ChildPath $file.wnr 

##account en passw. file1 creation
$mail= foreach($acount in $sa){
    "Beste $($account.aanvrager), `n `n Voor $($account.wnr) is een nieuwe serviceaccount aangemaakt. `n `n Accountnaam: $($account.sanaam) `n `n Wachtwoord : $($ `n `n mvg, `n `n Cloud AD team." | out-file -filepath $newpath1

#Text to add to the ticket in Topdesk
$Result = foreach($name in $sa){
"Het account '$($account.Sanaam)' is aangemaakt toegevoegd aan de Adgroep(en): `n $ADG `n de credentials zijn verstuurd aan $($account.aanvrager)" |  Set-Clipboard

Thank you for the time and the reply!

#Create a textfile with credentials ( fr a single account)

$newpath1  = foreach($file in $account){
join-path -Path "T:\Naar CW\"  -ChildPath $file.wnr 

With this code you’re creating a path but you don’t do nothing with it. If you want to create a file you should use

and provide the path you just created for it. :wink:

Hello Olaf,

Thanks for the reply,
Appreciate it.

I will use it as inspiration to improve my scripting knowledge