Read the user names from a file.
$AllUsers = Get-Content -Path "C:\data\users.txt"
foreach ($UserName in $AllUsers) {
"Adding $UserName"
New-Item "\\server\home\PIT\$UserName\Recover" -type directory -Force
}
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I would like to create a script to create a new folder called Recover in a new users home folder and using the inherited permissions. I was able to get the it to work by using the following code
$UserName = Read-Host -Prompt 'Enter User ID'
#$FolderName = Read-Host -Prompt 'Input Folder Name'
New-Item "\server\home\PIT\$UserName\Recover" -type directory -Force
but how would I get it to work without prompting for a username is this possible?
Read the user names from a file.
$AllUsers = Get-Content -Path "C:\data\users.txt"
foreach ($UserName in $AllUsers) {
"Adding $UserName"
New-Item "\\server\home\PIT\$UserName\Recover" -type directory -Force
}
The issue I am having I am trying to create a sub folder called Recover in a new user's home directory after the user created from ADUC but we use Manage Engines AD Manager Plus. The initial home drive user folder is created like normal using the \server\home\site code\%username%. When using the code below the subfolder never creates. Our home share is setup with 10 site code folders then the user folder inside of the parent. I don't understand what I would have to put in my code for it to read or know what site code\$username for the Subfolder Recover to auto create.
New-Item "\server\home\PIT\$UserName\Recover" -type directory -Force
I do not have an AD environment to test with, so you will need to get the Get-ADUser command working for your environment.
[Cmdletbinding()]
Param(
[string]$UserName = ""
)
$LogFile = "C:\temp\RecoverFolderCreation.log"
function LogIt($msg){
$msg # display if we are invoked interactively.
"$(get-date) - $msg" | Out-File $LogFile -Append
}
try {
if ($UserName -eq "") {
LogIt "No user name parameter was provided."
return
}
LogIt "RecoverFolderCreation invoked for $username"
$user = Get-ADUser -Identity $UserName -Properties sAMAccountName,HomeDirectory
$FolderName = $user.HomeDirectory + "\Recover"
LogIt "Creating $FolderName"
New-Item $FolderName -type directory -Force
} catch {
LogIt "We crashed."
LogIt $_
}