PowerShell/scripts/new-user.ps1

35 lines
893 B
PowerShell
Raw Permalink Normal View History

2024-10-01 15:11:03 +02:00
<#
2022-01-31 14:24:06 +01:00
.SYNOPSIS
2024-09-19 17:22:34 +02:00
Create a new user
2022-01-31 14:24:06 +01:00
.DESCRIPTION
2023-12-05 09:00:27 +01:00
This PowerShell script creates a new user account with an encrypted home directory.
2022-01-31 14:24:06 +01:00
.EXAMPLE
2023-12-05 09:00:27 +01:00
PS> ./new-user.ps1 Joe
2024-10-01 13:37:53 +02:00
Created user account 'Joe' with encrypted home directory in 11s.
2022-01-31 14:24:06 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2022-01-31 14:24:06 +01:00
#>
2023-12-05 09:00:27 +01:00
param([string]$username = "")
2022-01-31 14:24:06 +01:00
try {
2023-12-05 09:00:27 +01:00
if ($username -eq "") { $username = Read-Host "Enter the new user name" }
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2022-01-31 14:24:06 +01:00
if ($IsLinux) {
2024-09-19 17:21:59 +02:00
& sudo apt install ecryptfs-utils
2023-12-05 09:00:27 +01:00
& sudo adduser --encrypt-home $username
2022-01-31 14:24:06 +01:00
} else {
throw "Not supported yet"
}
2023-12-05 09:00:27 +01:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-10-01 13:37:53 +02:00
"✅ Created user account '$username' with encrypted home directory in $($elapsed)s."
2022-01-31 14:24:06 +01:00
exit 0 # success
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2022-01-31 14:24:06 +01:00
exit 1
}