Powershell v2.0 提取子串

Powershell v2.0 extract substring

我是使用 Powershell 的新手。我在使用 Powershell v2.0 的服务器上工作。

我有一个分配给子字符串的日期值 ($d1):2016-06-08-07.30.03.119000-240

我只想获取 date/time 部分:2016-06-08-07.30.03

我已经能够执行以下操作来获取:2016-06-08-07。 30. 03

echo $d1 > $tempfile
$d1a = ( Get-Content $tempfile | %{ $_.Split('.')[0]; } )
$d1b = ( Get-Content $tempfile | %{ $_.Split('.')[1]; } )
$d1c = ( Get-Content $tempfile | %{ $_.Split('.')[2]; } )
$d1 = "$d1a.$d1b.$d1c"

有没有办法在没有额外空格的情况下获得我想要的值?

我试过了:

$d1 = ( $d1a, $d1b, $d1c -join "." )

但这给出:System.Object[].System.Object[].System.Object[]

任何帮助将不胜感激。

您要提取的子字符串具有明确定义的长度(19 个字符),因此最简单的方法是使用 Substring() 方法:

$d1.Substring(0, 19)