Created
July 28, 2024 12:41
-
-
Save Joaquim3/474ec722118a27be6b0c10171d9ac1ae to your computer and use it in GitHub Desktop.
POWERSHELL ⇢ Create New Local User with credentials ⇢ Adds it to a Group ⇢ Creates and Shares its Folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #------------------------------------------------------ | |
| # This Creates New Local User with credentials ⇢ Adds it to a Group ⇢ Creates and Shares its Folder | |
| #------------------------------------------------------ | |
| #------------------------------------------------------ | |
| # Get Random String | |
| #------------------------------------------------------ | |
| Function Get-RandomAlphanumericString { | |
| [CmdletBinding()] | |
| Param ([int] $length = 8) | |
| Begin{ | |
| } | |
| Process{ | |
| Write-Output ( -join ((0x30..0x39) + ( 0x41..0x5A) + ( 0x61..0x7A) | Get-Random -Count $length | % {[char]$_}) ) | |
| } | |
| } | |
| #------------------------------------------------------ | |
| # Set folder Permissions for this LOCAL User | |
| #------------------------------------------------------ | |
| Function fncAssignFolderPermissionForUser() | |
| { | |
| $Acl = Get-Acl $FolderToCreate | |
| $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($NewLocalUser, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow") | |
| $Acl.SetAccessRule($Ar) | |
| Set-Acl $FolderToCreate $Acl | |
| } | |
| #------------------------------------------------------ | |
| # Create New LOCAL User Here | |
| # Change "***MYGROUP***" by your defined group | |
| # "***MYNEWUSER***" by your new LOCAL User | |
| # "***MYNEWUSERFULLNAME***" by your new LOCAL User Full name | |
| #------------------------------------------------------ | |
| $NewLocalUser = "***MYNEWUSER***" | |
| $NewLocalUserFull = "***MYNEWUSERFULLNAME***" | |
| $MyGroup = "***MYGROUP***" | |
| $Password = (Get-RandomAlphanumericString -length 11) | |
| # Write-Output $Password | |
| $Passwordsec = ConvertTo-SecureString $Password -AsPlainText -Force | |
| New-LocalUser $NewLocalUser -Password $Passwordsec -FullName $NewLocalUserFull | |
| Add-LocalGroupMember -Group $MyGroup -Member $NewLocalUser | |
| Set-LocalUser -Name $NewLocalUser -PasswordNeverExpires 1 -UserMayChangePassword 0 | |
| #------------------------------------------------------ | |
| # Create Local Folder for this LOCAL User | |
| #------------------------------------------------------ | |
| $FolderToCreate = "c:\users\" + $NewLocalUser | |
| if (!(Test-Path $FolderToCreate -PathType Container)) { | |
| $null = New-Item -ItemType Directory -Force -Path $FolderToCreate | |
| } | |
| #------------------------------------------------------ | |
| # Set folder Permissions for this LOCAL User | |
| #------------------------------------------------------ | |
| fncAssignFolderPermissionForUser | |
| #------------------------------------------------------ | |
| # Share the Folder with this new LOCAL User | |
| #------------------------------------------------------ | |
| $ShareName = $NewLocalUser.replace(".","") | |
| New-SMBShare –Name $ShareName –Path $FolderToCreate –FullAccess $NewLocalUser | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment