mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-08-23 16:12:08 +00:00
Add comprehensive test suite, fix minor issues, rename function and file names to match approved verbs (#708)
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
# Returns the directory path of the specified user, exits script if user path can't be found
|
||||
function Get-UserDirectory {
|
||||
param (
|
||||
$userName,
|
||||
$fileName = "",
|
||||
$exitIfPathNotFound = $true
|
||||
)
|
||||
|
||||
try {
|
||||
if ($userName -eq "*") {
|
||||
$rootPaths = @(
|
||||
(Join-Path $env:SystemDrive 'Users')
|
||||
(Split-Path -Path $env:USERPROFILE -Parent)
|
||||
) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | Select-Object -Unique
|
||||
|
||||
foreach ($rootPath in $rootPaths) {
|
||||
if (-not (Test-Path -LiteralPath $rootPath -PathType Container)) {
|
||||
continue
|
||||
}
|
||||
|
||||
$wildcardPath = if ([string]::IsNullOrWhiteSpace($fileName)) {
|
||||
Join-Path $rootPath '*'
|
||||
}
|
||||
else {
|
||||
Join-Path (Join-Path $rootPath '*') $fileName
|
||||
}
|
||||
|
||||
return $wildcardPath
|
||||
}
|
||||
}
|
||||
|
||||
$userContext = Resolve-UserProfileContext -UserName $userName
|
||||
$resolvedUserDirectory = if ($userContext) { $userContext.ProfilePath } else { $null }
|
||||
if ($resolvedUserDirectory) {
|
||||
$userPath = if ([string]::IsNullOrWhiteSpace($fileName)) {
|
||||
$resolvedUserDirectory
|
||||
}
|
||||
else {
|
||||
Join-Path $resolvedUserDirectory $fileName
|
||||
}
|
||||
|
||||
if ((Test-Path -LiteralPath $userPath) -or ((Test-Path -LiteralPath $resolvedUserDirectory -PathType Container) -and (-not $exitIfPathNotFound))) {
|
||||
return $userPath
|
||||
}
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Error "Something went wrong when trying to find the user directory path for user $userName. Please ensure the user exists on this system"
|
||||
Wait-ForKeyPress
|
||||
}
|
||||
|
||||
Write-Error "Unable to find user directory path for user $userName"
|
||||
Wait-ForKeyPress
|
||||
}
|
||||
Reference in New Issue
Block a user