diff --git a/Formatting/Posh/Posh.Profiles.format.ps1 b/Formatting/Posh/Posh.Profiles.format.ps1 new file mode 100644 index 0000000..e02d667 --- /dev/null +++ b/Formatting/Posh/Posh.Profiles.format.ps1 @@ -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()" + } +} \ No newline at end of file diff --git a/Types/Posh.Profiles/Add.ps1 b/Types/Posh.Profiles/Add.ps1 new file mode 100644 index 0000000..cc717de --- /dev/null +++ b/Types/Posh.Profiles/Add.ps1 @@ -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 + + diff --git a/Types/Posh.Profiles/README.md b/Types/Posh.Profiles/README.md new file mode 100644 index 0000000..4a618c2 --- /dev/null +++ b/Types/Posh.Profiles/README.md @@ -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 \ No newline at end of file diff --git a/Types/Posh.Profiles/Remove.ps1 b/Types/Posh.Profiles/Remove.ps1 new file mode 100644 index 0000000..c7c299a --- /dev/null +++ b/Types/Posh.Profiles/Remove.ps1 @@ -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 \ No newline at end of file diff --git a/Types/Posh.Profiles/get_Current.ps1 b/Types/Posh.Profiles/get_Current.ps1 new file mode 100644 index 0000000..cdc32e3 --- /dev/null +++ b/Types/Posh.Profiles/get_Current.ps1 @@ -0,0 +1,9 @@ +<# +.SYNOPSIS + Gets the current Profile +.DESCRIPTION + Gets the current PowerShell Profile +.EXAMPLE + $posh.Profile.Current +#> +"$profile" diff --git a/Types/Posh.Profiles/get_Files.ps1 b/Types/Posh.Profiles/get_Files.ps1 new file mode 100644 index 0000000..1380410 --- /dev/null +++ b/Types/Posh.Profiles/get_Files.ps1 @@ -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 + } +} \ No newline at end of file