递归将文件添加到文件夹并创建文件夹(如果不存在)



我正在尝试递归地将配置文件添加到以下路径:C:\Users*\AppData\Roaming\

这是我到目前为止的代码:

#Sets config file as source.
$Source = "$dirFilesNewAgentAPAgentAP.cfg"
#Recursive copying of a file to specific folder destination.
$Destination = 'C:Users*AppDataRoamingTrioTrio Enterprise'
Get-ChildItem $Destination | ForEach-Object {Copy-Item -Path $Source -Destination $_ -Force}

我希望 powershell 脚本为每个用户添加路径(如果他们还没有(,然后添加.cfg文件。 我尝试搜索问题但没有运气。

我认为这里一个好的解决方案是迭代所有用户文件夹,确保目标路径存在,然后执行复制。

#Sets config file as source.
$Source = "$dirFilesNewAgentAPAgentAP.cfg"
#Recursive copying of a file to specific folder destination.
$Destination = 'AppDataRoamingTrioTrio Enterprise'
Get-ChildItem C:Users* -Directory | ForEach-Object {New-Item -Path (Join-Path $_.FullName $Destination) -ItemType "directory" -Force | Copy-Item -Path $Source -Destination $_ -Force}
使用New-Item创建文件夹,-Force

开关将导致它创建文件夹(如果不存在(并传递文件夹对象,或者如果它确实存在,则只传递文件夹对象而不执行任何其他操作。

相关内容

最新更新