16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PowerShell】全角⇔半角相互変換

Last updated at Posted at 2016-05-29
半角→全角
Add-Type -AssemblyName Microsoft.VisualBasic
using namespace Microsoft.VisualBasic

$narrow = "wide_case"
$wide = [Strings]::StrConv($narrow, [VbStrConv]::Wide)

$wide #=> wide_case
全角→半角
Add-Type -AssemblyName Microsoft.VisualBasic
using namespace Microsoft.VisualBasic

$wide = "narrow_case"
$narrow = [Strings]::StrConv($wide, [VbStrConv]::Narrow)

$narrow #=> narrow_case
  • PowerShell 5.0からusing namespaceが使えるようになった。それより前のバージョンでは、クラスを使用するときは完全装飾名でなければならない。
  • Add-Typeコマンドレットは、PowerShellで使用できるクラスを増やすコマンドレット。アセンブリ名を指定すれば、そのアセンブリ内のクラスが使えるようになる。他にもC#やVisualBasicのコード(メソッドだけでも可)からクラスを生成したり、DLLファイルのパスを指定してクラスを追加することができる。多機能。

上記コードのコマンドレット版

Add-Type -AssemblyName Microsoft.VisualBasic

function ConvertTo-Wide
{
    param
    (
        [Parameter(ValueFromPipeline=$true)]
        [string]
        $String
    )

    process
    {
        [Microsoft.VisualBasic.Strings]::StrConv($String, [Microsoft.VisualBasic.VbStrConv]::Wide)
    }
}

function ConvertTo-Narrow
{
    param
    (
        [Parameter(ValueFromPipeline=$true)]
        [string]
        $String
    )

    process
    {
        [Microsoft.VisualBasic.Strings]::StrConv($String, [Microsoft.VisualBasic.VbStrConv]::Narrow)
    }
}
16
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?