0

我想分配当前目录的名称来构造并行目录的路径(运行一些 diff 命令)。

但是,当我这样做时:

New-Item -ItemType Directory -Name my_test_dir;
Set-Location my_test_dir;
$a = $( Get-Item . | Select-Object Name ); 
write-host( "x${a}x" );

我明白了

x@{Name=my_test_dir}x

而不是我所期望的:

xmy_test_dirx

那么,我如何“拆箱”目录的名称呢?


PS - 为了便于测试,我使用:

mkdir my_test_dir; cd my_test_dir; $a = $( Get-Item . | Select Name ); echo "x${a}x"; cd ..; rmdir my_test_dir
4

1 回答 1

1

当您使用... |Select-Object PropertyName时,它会生成一个具有名为 的属性的对象,并PropertyName从输入项的相应属性中复制值。

使用Select-Object -ExpandProperty PropertyNameorForEach-Object MemberName来获取属性的值:

$a = Get-Item . | Select-Object -ExpandProperty Name 
# or 
$a = Get-Item . | ForEach-Object Name

...或直接引用该属性:

$a = (Get-Item .).Name
于 2020-06-01T15:22:13.447 回答