potisanのプログラミングメモ

趣味のプログラマーがプログラミング関係で気になったことや調べたことをいつでも忘れられるようにメモするブログです。はてなブログ無料版なので記事の上の方はたぶん広告です。記事中にも広告挿入されるみたいです。

PowerShell 7 小物類

PowerShell7の短いコードや覚書です。

有用なページ

文字列配列からURLとして不正な要素を除外する

Add-Type -Assembly System.Web
$values1 = "あいうえお", "abc", "012"
$values2 = ($values1 | Where-Object {$_ -eq [System.Web.HttpUtility]::UrlEncode($_)})
$values2
#abc
#012

PowerShellシステム変数の取得

Get-Variable
<#
Name                           Value
----                           -----
$
?                              True
^
args                           {}
ConfirmPreference              High
DebugPreference                SilentlyContinue
EnabledExperimentalFeatures    {}
Error                          {}
ErrorActionPreference          Continue
ErrorView                      ConciseView
ExecutionContext               System.Management.Automation.EngineIntrinsics
false                          False
FormatEnumerationLimit         4
HOME                           <削除しました>
Host                           System.Management.Automation.Internal.Host.Inte…InformationPreference          SilentlyContinue
input                          System.Collections.ArrayList+ArrayListEnumerato…IsCoreCLR                      True
IsLinux                        False
IsMacOS                        False
IsWindows                      True
MaximumHistoryCount            4096
MyInvocation                   System.Management.Automation.InvocationInfo
NestedPromptLevel              0
null
OutputEncoding                 System.Text.UTF8Encoding
PID                            8480
PROFILE                        <削除しました>\Microsoft.Po…ProgressPreference                                 Continue
PSBoundParameters              {}
PSCommandPath
PSCulture                      ja-JP
PSDefaultParameterValues       {}
PSEdition                      Core
PSEmailServer
PSHOME                         <PowerShellの場所>
PSScriptRoot
PSSessionApplicationName       wsman
PSSessionConfigurationName     http://schemas.microsoft.com/powershell/Microso…PSSessionOption                System.Management.Automation.Remoting.PSSession…PSUICulture                    ja-JP
PSVersionTable                 {PSVersion, PSEdition, GitCommitId, OS…}
PWD                            <削除しました>
ShellId                        Microsoft.PowerShell
StackTrace
true                           True
VerbosePreference              SilentlyContinue
WarningPreference              Continue
WhatIfPreference               False
#>

変数ドライブを使う場合

Get-ChildItem variable:

変数を使用したオブジェクトの保存

環境変数の取得

#全部取得
Get-ChildItem env:
#個別取得
$env:SystemRoot