Add scripts for editing the powershell profile

This commit is contained in:
Manuel Thalmann 2024-03-20 16:33:18 +01:00
parent 638d241973
commit 5055eddb82
2 changed files with 60 additions and 24 deletions
scripts/Windows/Scripts

View file

@ -1,4 +1,6 @@
#!/bin/pwsh
. "$PSScriptRoot/../../Common/Config/powershell/profile.ps1";
class Context {
[string]$EntryPoint;
[string]$RootDir;
@ -47,41 +49,29 @@ class Context {
}
[void] AddPowerShellProfileStatement([bool] $defaultUser, [string] $category, [string] $statement) {
[string] $userDir = $null;
[string] $homeDir = $null;
if ($defaultUser) {
$userDir = "C:\Users\Default";
$homeDir = "C:\Users\Default";
} else {
$userDir = "~";
$homeDir = "~";
}
$this.AddPowerShellProfileStatement($userDir, $category, $statement);
$this.AddPowerShellProfileStatement($homeDir, $category, $statement);
}
[void] AddPowerShellProfileStatement([string] $userDir, [string] $category, [string] $statement) {
Push-Location ~;
$profileFiles = @((powershell -c '$PROFILE'), (pwsh -c '$PROFILE')) | ForEach-Object { Resolve-Path -Relative $_ };
[void] AddPowerShellProfileStatement([string] $homeDir, [string] $category, [string] $statement) {
if ($category) {
$profileFiles = $profileFiles | ForEach-Object { Join-Path (Split-Path -Parent $_) "profile.d" "$category.ps1"; };
$overwrite = $true;
} else {
$overwrite = $false;
}
$profileFiles | ForEach-Object {
$fileName = "$userDir\$_";
$dirName = Split-Path -Parent $fileName;
$this.AddPowerShellProfileStatement($homeDir, $category, $statement, $overwrite);
}
if (-not (Test-Path -PathType Container $dirName)) {
New-Item -ItemType Directory $dirName;
}
if (Test-Path -PathType Leaf $fileName) {
Add-Content -Force "$fileName" "`n$statement";
} else {
Set-Content -Force "$fileName" "$statement";
}
};
Pop-Location;
[void] AddPowerShellProfileStatement([string] $homeDir, [string] $category, [string] $statement, [bool] $overwrite) {
Add-PowerShellProfileStatement -HomeDir $homeDir -Category $category -Statement $statement -Overwrite $overwrite;
}
[Microsoft.Win32.RegistryKey] EnsureConfigKey() {