Skip to content

Instantly share code, notes, and snippets.

@ritalin
Last active July 6, 2018 02:33
Show Gist options
  • Save ritalin/5403657 to your computer and use it in GitHub Desktop.
Save ritalin/5403657 to your computer and use it in GitHub Desktop.
ものぐさな自分用、Powershell設定
function prompt {
(Get-Host).UI.RawUI.WindowTitle = "PwoerShell - $pwd"
$name = (new-object "IO.FileInfo" $pwd.ProviderPath).name
"PS ${name}> "
}
function path { $env:path.split(";", [StringSplitOptions]::RemoveEmptyEntries) }
function set-env([string]$key, [string]$value) {
if ([string]::IsNullOrEmpty($key)) {
throw "環境変数のキーが指定されていません。"
}
if ($key.ToLower() -eq "path") {
throw "環境変数:pathを変更することはできません。"
}
if ([string]::IsNullOrEmpty($value)) {
throw "環境変数の値が指定されていません。"
}
[Environment]::SetEnvironmentVariable($key, $value, "User")
$cur = pwd
try {
cd env:
set-item -path $key -value $value
}
finally {
cd $cur
}
}
function rm-env([string]$key) {
if ([string]::IsNullOrEmpty($key)) {
throw "環境変数のキーが指定されていません。"
}
if ($key.ToLower() -eq "path") {
throw "環境変数:pathを削除することはできません。"
}
[Environment]::SetEnvironmentVariable($key, "", "User")
$cur = pwd
try {
cd env:
remove-item -path $key
}
finally {
cd $cur
}
}
function resolve-env([string] $val) { [Environment]::ExpandEnvironmentVariables($val) }
function add-path([string[]]$paths) {
$p1 = New-Object 'System.Collections.Generic.Dictionary[string, string]'
foreach ($p in $(path)) { $p1.add($p, $null) | out-null }
foreach ($p in $paths) { $p1[$(resolve-env $p)] = $null }
$env:path = [string]::join(";", (@() + $p1.keys))
}
function open {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string[]]$Path
)
process {
foreach ($p in $Path) {
if ($p.ToLower() -match '^(shell|http|https):') {
Write-Verbose "Invoke by explorer.exe $p"
& explorer $p
}
else {
Write-Verbose "call Invoke-Item $p"
invoke-Item $p
}
}
}
}
add-path @(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment