MEMO about PowerShell

バージョンを確認する

> $Host


Name             : Windows PowerShell ISE Host
Version          : 5.1.15063.786
InstanceId       : a95bbd00-1e56-4bc0-90aa-8e3ba0333e1a
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : ja-JP
CurrentUICulture : ja-JP
PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
> $PSVersionTable

Name                           Value                  
----                           -----                  
PSVersion                      5.1.15063.786          
PSEdition                      Desktop                
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.786         
CLRVersion                     4.0.30319.42000        
WSManStackVersion              3.0                    
PSRemotingProtocolVersion      2.3                    
SerializationVersion           1.1.0.1

アセンブリ(DLL)をロードする

> [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

GAC    Version        Location                                                                          
---    -------        --------                                                                          
True   v4.0.30319     C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b...

ロード済みアセンブリの確認
> [AppDomain]::CurrentDomain.GetAssemblies() 

> [AppDomain]::CurrentDomain.GetAssemblies() | %{$_.GetName().Name}

> [AppDomain]::CurrentDomain.GetAssemblies() | Format-List

ファイル名を一括変換する

> ls|Sort-Object {$_.Name}|Rename-Item {'aaa'+$_.Name.Substring(0,2)+'.mp4'}

Windowsフォームを作成する

# アセンブリのロード
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

# フォーム
$form = New-Object -TypeName System.Windows.Forms.Form

# コントロール
$wb = New-Object -TypeName System.Windows.Forms.WebBrowser
$wb.Dock = [System.Windows.Forms.DockStyle]::Fill
$wb.Url = New-Object -TypeName System.Uri("http://www.yahoo.co.jp")
$form.Controls.Add($wb)

# フォームを表示
$form.ShowDialog()

Process

using namespace System.Diagnostics

function MyProcess {
    Param (
        [string]$FilePath,
        [string]$ArgumentList,
        [bool]$NoNewWindow = $true
    )

    try {
        $p = [Process]::new()
        $p.StartInfo.UseShellExecute = $false
        $p.StartInfo.RedirectStandardOutput = $true
        $p.StartInfo.RedirectStandardError = $true
        $p.StartInfo.CreateNoWindow = $NoNewWindow
        $p.StartInfo.FileName = $FilePath
        $p.StartInfo.Arguments = $ArgumentList
        $p.Start() | Out-Null
        $stdout = $p.StandardOutput.ReadToEnd()
        $stderr = $p.StandardError.ReadToEnd()
        $p.WaitForExit()

        if ($stdout.Length -gt 0) {
            Write-Output $stdout
        }

        if ($stderr.Length -gt 0) {
            Write-Error $stderr
        }

        exit $p.ExitCode
    } finally {
        $p.Dispose()
        exit 9
    } 
}