PowerShell - Regexp

Windows Powershell Menu

About

Multilingual Regular Expression Syntax (Pattern) in Powershell

Example

Escape

  • With Regexp, you need to use a single quote
'`[Hello`]'

Filtering

  • Extracting fmw_12.2.1.3.0_infrastructure from a file
$VALUE = get-content $META_FILE | Select-String "fmw.*infrastructure" -AllMatches | % { $_.Matches } | %{ $_.Value }
  • Extract with group name
$VALUE = gc $META_FILE | Select-String "fmw_(?<version>.*)_infrastructure" -AllMatches | % { $_.Matches.Groups[1].Value }

Condition

if ($fileName -match '.*zip') {

            $sourceFile="$targetDir\$fileName"
            Write-host "Unzip file", $sourceFile
            echo Expand-Archive $sourceFile -DestinationPath $targetDir -force

}







Share this page:
Follow us:
Task Runner