Powershell循环通过格式表

Ale*_*ula 2 powershell foreach formattable

我有一个问题.我创建了一个包含文件名,源目录和目标目录的格式表.现在我尝试用foreach遍历表格.在这个循环中,我想将文件从源目录移动到目标目录.我的问题是从行中获取项目.

这是我的示例代码:

cls
$MovePathSource = "C:\Users\user\Desktop\sourcefolder"
$MovePathDestination = "C:\Users\user\Desktop\destinationfolder"

$filetypes = @("*.llla" , "html")
$table = dir $MovePathSource -Recurse -Include $filetypes | Format-Table@{Expression={$_.Name};Label="Filename"},@{Expression={($_.DirectoryName)};Label="Sourcepath"},@{Expression={($_.DirectoryName).Replace($MovePathSource,$MovePathDestination)};Label="Destinationpath"}

$table

foreach ($row in $table)
{
write-host "$row.Sourcepath"
#Move-Item -Path ($row.Sourcepath + "\" + $row.Filename) -Destination $row.Destinationpath
}
Run Code Online (Sandbox Code Playgroud)

Fro*_* F. 6

Format-*在完成数据之前,切勿使用-cmdlet.即便如此,只有在向用户显示内容(或创建邮件等)时才使用它,因为它们会破坏原始数据并且只留下特殊的格式对象.

更换Format-TableSelect-Object得到相同的结果,同时保持可重用的对象.

$table = dir $MovePathSource -Recurse -Include $filetypes |
Select-Object @{Expression={$_.Name};Label="Filename"},@{Expression={($_.DirectoryName)};Label="Sourcepath"},@{Expression={($_.DirectoryName).Replace($MovePathSource,$MovePathDestination)};Label="Destinationpath"}
Run Code Online (Sandbox Code Playgroud)