Tag Archives: PowerShell Programming language

PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell and the associated scripting language. Platforms are Windows PowerShell .NET Framework, Implementation language on C#.

Automatically create users in bulk in PowerShell

Create users in bulk automatically in PowerShell while read from file

If users are to be create from a bulk automatically, a small PowerShell script help to do it. Here the users are created from an Excel sheet. The user names are in column (A) and the password contains the second column (B), the table is exported and saved as a CSV file.

For example, the table is saved as “list.txt” via File – Export – Change File Type – CSV (Delimited) (*.csv).

The foreach loop reads the contents of the file “list.txt” and writes the content of the first column to the variable $user, the second column is assigned to $pass. To create users in bulk run this commands in the PowerShell as Administrator.

foreach ($line in Get-Content ".\list.txt") {
    $user,$pass = $line -split ';'
    New-LocalUser $user -Password (ConvertTo-SecureString $pass -AsPlainText -Force) -FullName $user
}

The CSV file “list.txt” contains the users and semicolon separately the password. To allow the password as PlainText format, the ConvertTo-SecureString cmdlet must be passed with -AsPlainText -Force.

Note. To make scripts executable, the command Set-ExecutionPolicy RemoteSigned must be executed.

If you want to prevent the users from changing the password, the following options must be specified.

foreach ($line in Get-Content ".\list.txt") {
    $user,$pass = $line -split ';'
    New-LocalUser $user -Password (ConvertTo-SecureString $pass -AsPlainText -Force) -FullName $user -Description $user -PasswordNeverExpires -UserMayNotChangePassword
}

The script runs in the PowerShell in the same directory where the CSV file “list.txt” is located. To create users in bulk.

PS C:\> .\adduser.ps1

The users created in this way should still belong to a group, in the following loop the users are added to the users group.

foreach ($line in Get-Content ".\list.txt") {
    $user,$pass = $line -split ';'
    Add-LocalGroupMember -Group "User" -Member $user
}

If you would like to remove the users, use this loop again.

foreach ($line in Get-Content ".\list.txt") {
    $user,$pass = $line -split ';'
    Remove-LocalUser -Name $user
}

Note. If the users are to be created in an AD domain, the New-ADUser cmdlet is responsible.

Conclusion

In this Tutorial you will learn, how to create users in bulk automatically, a small PowerShell script help to do it. Here the users are created from an Excel sheet.

Network drive mapping from PowerShell Script

Mapping Windows Network Drive using PowerShell

PowerShell scripts can be used to mapping network drives to Windows network shares on servers or NAS devices. Where batch processing is not the right choice, or where Group Policy is not possible. A PowerShell script can perform this task. For example, when running login scripts with remote clients via VPN. Or clients that are not members of the AD domain.

PS-Script for Network Drive Mapping

This PowerShell Script example netdrive.ps1, creates the Windows network mapping to drive Z: for a specific user, whereby you can log on as a different user than you are authenticated with on the client.

$LogFile = "netdrive.log"
Get-Date -Format "MM/dd/yyyy HH:mm" | Out-File $LogFile -Force
Write-Host "`nEnter your network credentials to map network drives or cancel mapping" -ForegroundColor Yellow
$Cred = Get-Credential -Message "Please enter your credentials" -UserName "domain\username"
if ($Cred) {
    $sharePath = "\\server\share"
    $mapDrive = "Z"
    Write-Host "`nMapping network share.." -ForegroundColor White
    Add-Content $LogFile -Value "Mapping drive $mapDrive $sharePath"
    New-PSDrive -Name $mapDrive -Root $sharePath -Persist -Scope Global -PSProvider "FileSystem" -Credential $Cred | Out-File $LogFile -Append
} else {
    Add-Content $LogFile -Value "No credentials were given, network mapping canceled."
}

The corresponding Windows network share is defined in line 6 at $sharePath, and the drive is assigned in line 7 at $mapDrive, change the respective placeholder between the quotation marks. If further network shares are to be mapped, in the if loop copy lines 6 – 10 for the next network share using the variables $sharePath1 and $mapDrive1.

Run the script in the PowerShell with change to directory in which you placed the netdrive.ps1 script file.

.\netdrive.ps1

Replace the placeholders domain\username with the appropriate domain and user, if there is no domain, then just leave it blank.

Windows PowerShell Credentials
Windows PowerShell Credentials

Run PowerShell Script from Batch

If a batch is already in place, for example netlogon.bat, then execute netdrive.ps1 out from a batch file as follows:

start /wait "" powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File netdrive.ps1

After a successful logon, is it possible to check the network mapping, for this the processes are logged in the file netdrive.log.

Conclusion

In this Tutorial you will learn, how to using PowerShell scripts that can be used to mapping network drives. To Windows network shares on servers or NAS devices. Where batch processing is not the right choice, or where Group Policy is not possible.