-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Brundage
committed
Jul 20, 2023
1 parent
64e8c24
commit 9200a2d
Showing
6 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Write-FormatView -TypeName Posh.Profiles -Action { | ||
Write-FormatViewExpression -ScriptBlock { | ||
Show-Markdown -InputObject $_.README | ||
} | ||
|
||
Write-FormatViewExpression -Newline | ||
Write-FormatViewExpression -Newline | ||
|
||
Write-FormatViewExpression -Text "The current profile is:" | ||
|
||
Write-FormatViewExpression -Newline | ||
Write-FormatViewExpression -Newline | ||
|
||
|
||
Write-FormatViewExpression -Style Italic -Property Current | ||
|
||
Write-FormatViewExpression -Newline | ||
Write-FormatViewExpression -Newline | ||
|
||
|
||
Write-FormatViewExpression -Style 'Foreground.Cyan' -ScriptBlock { | ||
"To add to your profile, use `$posh.Profile.Add()" | ||
} | ||
|
||
Write-FormatViewExpression -Newline | ||
|
||
Write-FormatViewExpression -Style 'Foreground.Cyan' -ScriptBlock { | ||
"To remove from your profile, use `$posh.Profile.Remove()" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<# | ||
.SYNOPSIS | ||
Adds content to a profile | ||
.DESCRIPTION | ||
Adds content to a PowerShell profile | ||
#> | ||
param( | ||
# The content to add to your profile. | ||
[ScriptBlock] | ||
$Content, | ||
|
||
# Which profile should the content be added to? | ||
# The default is the current user's current host. | ||
[ValidateSet('AllUsersAllHosts','AllUsersCurrentHosts','CurrentUserAllHosts','CurrentUserCurrentHost')] | ||
[string] | ||
$WhichProfile = 'CurrentUserCurrentHost' | ||
) | ||
|
||
$profilePath = $PROFILE.$WhichProfile | ||
|
||
$profileContent = | ||
if (Test-Path $profilePath) { | ||
Get-Content -Raw $profilePath | ||
} else { | ||
'' | ||
} | ||
|
||
if ($profileContent -like "*$Content*") { | ||
return | ||
} | ||
|
||
$profileContent += [Environment]::NewLine | ||
$profileContent += "$content" | ||
$profileContent | Set-Content -Path $profilePath -PassThru | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
PowerShell has profiles! | ||
|
||
Profiles run whenever PowerShell is loaded. | ||
|
||
They're great if there's a module you want to always load, or a variable you always want defined. | ||
|
||
To see how to manipulate PowerShell profiles with Posh, use $posh.Profiles | Get-Member |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<# | ||
.SYNOPSIS | ||
Removes content from a profile | ||
.DESCRIPTION | ||
Removes content from a PowerShell profile. | ||
#> | ||
param( | ||
# The content to remove. | ||
# If the content is a regular expression pattern, any matches will be removed. | ||
# Otherwise, any case-insensitive occurances of the string will be removed. | ||
$Content, | ||
|
||
# Which profile should the content be removed from? | ||
# The default is the current user's current host. | ||
[ValidateSet('AllUsersAllHosts','AllUsersCurrentHosts','CurrentUserAllHosts','CurrentUserCurrentHost')] | ||
[string] | ||
$WhichProfile = 'CurrentUserCurrentHost' | ||
) | ||
|
||
$profilePath = $PROFILE.$WhichProfile | ||
|
||
$profileContent = | ||
if (Test-Path $profilePath) { | ||
Get-Content -Raw $profilePath | ||
} else { | ||
'' | ||
} | ||
|
||
if ($Content -is [regex]) { | ||
$profileContent = $Content.Replace($profileContent, '') | ||
} else { | ||
$Content = "$content" | ||
if ($Content) { | ||
$profileContent = | ||
$profileContent.Replace( | ||
$content, '', [StringComparison]::OrdinalIgnoreCase | ||
) | ||
} | ||
} | ||
|
||
$profileContent | Set-Content -Path $profilePath -PassThru |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<# | ||
.SYNOPSIS | ||
Gets the current Profile | ||
.DESCRIPTION | ||
Gets the current PowerShell Profile | ||
.EXAMPLE | ||
$posh.Profile.Current | ||
#> | ||
"$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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<# | ||
.SYNOPSIS | ||
Gets the profile files | ||
.DESCRIPTION | ||
Gets the FileInfo objects for each profile that exists. | ||
.EXAMPLE | ||
$posh.Profile.Files | ||
#> | ||
foreach ($potentialPath in $profile.CurrentUserCurrentHost, $profile.CurrentUserAllHosts, $profile.AllUsersAllHosts, $profile.AllUsersCurrentHost) { | ||
if ([IO.File]::Exists($potentialPath)) { | ||
[IO.FileInfo]$potentialPath | ||
} | ||
} |