-
-
Save mavericksevmont/68011c52e67197f5c9c54508cd4b641e to your computer and use it in GitHub Desktop.
PowerShell functions to create a new user profile
This file contains 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
<# | |
.Synopsis | |
Rough PS functions to create new user profiles | |
.DESCRIPTION | |
Call the Create-NewProfile function directly to create a new profile | |
.EXAMPLE | |
Create-NewProfile -Username 'testUser1' -Password 'testUser1' | |
.NOTES | |
Created by: Josh Rickard (@MS_dministrator) | |
Date: 24MAR2017 | |
Location: https://gist.github.com/MSAdministrator/41df43e780993e48bf637a4ccd0e4c68 | |
Contact: https://github.com/MSAdministrator | |
MSAdministrator.com | |
Modified by: Erick Sevilla (@MaverickSevmont) | |
Date: 28June2022 | |
Changes: Moved all functions to Create-NewProfile function's scope | |
#> | |
#Main function to create the new user profile | |
function Create-NewProfile { | |
[CmdletBinding()] | |
[Alias()] | |
[OutputType([int])] | |
Param | |
( | |
# Param1 help description | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=0)] | |
[string]$UserName, | |
# Param2 help description | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=1)] | |
[string] | |
$Password | |
) | |
Write-Verbose "Loading required functions" | |
Write-Verbose "Loading New-LocalUser" | |
#function to create the new local user first | |
function New-LocalUser | |
{ | |
[CmdletBinding()] | |
[Alias()] | |
[OutputType([int])] | |
Param | |
( | |
# Param1 help description | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=0)] | |
$userName, | |
# Param2 help description | |
[string] | |
$password | |
) | |
$system = [ADSI]"WinNT://$env:COMPUTERNAME"; | |
$user = $system.Create("user",$userName); | |
$user.SetPassword($password); | |
$user.SetInfo(); | |
$flag = $user.UserFlags.value -bor 0x10000; | |
$user.put("userflags",$flag); | |
$user.SetInfo(); | |
$group = [ADSI]("WinNT://$env:COMPUTERNAME/Users"); | |
$group.PSBase.Invoke("Add", $user.PSBase.Path); | |
} | |
Write-Verbose "Loading Register-NativeMethod" | |
#function to register a native method | |
function Register-NativeMethod | |
{ | |
[CmdletBinding()] | |
[Alias()] | |
[OutputType([int])] | |
Param | |
( | |
# Param1 help description | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=0)] | |
[string]$dll, | |
# Param2 help description | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=1)] | |
[string] | |
$methodSignature | |
) | |
$script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; } | |
} | |
Write-Verbose "Loading Add-NativeMethods" | |
#function to add native method | |
function Add-NativeMethods | |
{ | |
[CmdletBinding()] | |
[Alias()] | |
[OutputType([int])] | |
Param() | |
$nativeMethodsCode = $script:nativeMethods | % { " | |
[DllImport(`"$($_.Dll)`")] | |
public static extern $($_.Signature); | |
" } | |
Add-Type @" | |
using System; | |
using System.Text; | |
using System.Runtime.InteropServices; | |
public static class NativeMethods { | |
$nativeMethodsCode | |
} | |
"@ | |
} | |
Write-Verbose "Creating local user $Username"; | |
try | |
{ | |
New-LocalUser $UserName $Password; | |
} | |
catch | |
{ | |
Write-Error $_.Exception.Message; | |
break; | |
} | |
$script:nativeMethods = @(); | |
if (-not ([System.Management.Automation.PSTypeName]'NativeMethods').Type) | |
{ | |
Register-NativeMethod "userenv.dll" "int CreateProfile([MarshalAs(UnmanagedType.LPWStr)] string pszUserSid,` | |
[MarshalAs(UnmanagedType.LPWStr)] string pszUserName,` | |
[Out][MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszProfilePath, uint cchProfilePath)"; | |
Add-NativeMethods; | |
} | |
$localUser = New-Object System.Security.Principal.NTAccount("$UserName"); | |
$userSID = $localUser.Translate([System.Security.Principal.SecurityIdentifier]); | |
$sb = new-object System.Text.StringBuilder(260); | |
$pathLen = $sb.Capacity; | |
Write-Verbose "Creating user profile for $Username"; | |
try | |
{ | |
[NativeMethods]::CreateProfile($userSID.Value, $Username, $sb, $pathLen) | Out-Null; | |
} | |
catch | |
{ | |
Write-Error $_.Exception.Message; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changes: Moved all functions to Create-NewProfile function's scope