Powershell将文件复制到所有用户LOCALAPPDATA创建路径

时间:2018-07-05 03:14:12

标签: windows powershell copy

我搜集了一些脚本,但是我很难让它开始工作。

使用应用程序部署软件,我可以在部署后运行自定义脚本。

此应用程序正在将脚本和一堆文件复制到某个位置,然后再启动它们。 文件夹路径可能是随机的,所以我想使用脚本位置。

  • \路径
    • 脚本
    • 文件1
    • 文件2

我想将文件2复制到用户%LOCALAPPDATA%路径。该目录结构在Users AppData文件夹中尚不存在。 我需要在复制期间或复制之前创建文件夹结构。

C:\ users * \ AppData \ Local \ NewApplication \ Folder \

#Declare location of the XML file using spli-path. Copy XML file to all user %LOCALAPPDATA% paths and create folder structure
$scriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$Source = Join-Path $scriptPath 'Script.ps1'
$Destination = 'C:\users\*\AppData\Local\NewApplication\Folder\'
Get-ChildItem $Destination | ForEach-Object {Copy-Item -Path $Source -Destination $_ -Force -Recurse}
Exit $LASTEXITCODE

非常感谢您。

1 个答案:

答案 0 :(得分:0)

这应该为您完成。

$scriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent 
$Source = Join-Path $scriptPath 'File2.xml' 
$Destination = "$env:LOCALAPPDATA\New Application\Folder" 
if (!([system.io.directory]::Exists($Destination))){ 
    [system.io.directory]::CreateDirectory($Destination) 
} 
# This will only copy the XML file
Get-ChildItem $scriptPath | ForEach-Object {Copy-Item -Path $Source -Destination $Destination -Force -Recurse} 
# This will copy everything in the same directory as the script
#Get-ChildItem $scriptPath | ForEach-Object {Copy-Item -Path $_.Fullname -Destination $Destination -Force -Recurse} 
#Exit $LASTEXITCODE
相关问题