You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本复制文件到其他目录及报错解决求助

PowerShell脚本复制文件到C盘目录的问题解决

错误原因分析

  1. 目录创建时忽略了错误,导致目录未成功创建却继续执行复制操作
  2. 源文件使用相对路径,可能因工作目录不一致导致找不到文件
  3. 遍历用户目录的通配符使用不符合预期,无法正确定位目标路径
  4. 缺少管理员权限,无法访问或修改C盘用户目录下的系统相关路径

解决方案

1. 必须以管理员身份运行PowerShell

右键点击PowerShell,选择「以管理员身份运行」,否则无法修改C:\Users下的系统级目录。

2. 修改源文件为绝对路径

使用$PSScriptRoot获取脚本所在的U盘目录,拼接start.bin的绝对路径,避免相对路径的不确定性:

$Source = Join-Path $PSScriptRoot 'start.bin'

3. 修复目录创建逻辑

去掉错误静默忽略,改用-Force确保目录存在(不存在则创建,存在则不报错):

$defaultDestPath = 'C:\Users\Default\AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState'
New-Item -ItemType Directory -Path $defaultDestPath -Force | Out-Null

4. 正确遍历现有用户目录

先获取所有用户目录,再逐个拼接目标路径,确保每个路径存在后再复制:

$userDestTemplate = 'AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState'
Get-ChildItem 'C:\Users' -Directory | ForEach-Object {
    $destPath = Join-Path $_.FullName $userDestTemplate
    New-Item -ItemType Directory -Path $destPath -Force | Out-Null
    Copy-Item -Path $Source -Destination $destPath -Force
}

完整修改后的脚本

# Copy the start.bin file into all existing profiles, to overwrite all the crap MS is including in the default Start Menu
# Won't prevent users from unpinning these items or pinning new items, only handles initial cleanup
Write-Host ""
Write-Host "Clean up new and existing Start Menu's"

# 定义源文件绝对路径(自动获取脚本所在U盘目录)
$Source = Join-Path $PSScriptRoot 'start.bin'

# 处理Default用户的Start Menu目录
$defaultDestPath = 'C:\Users\Default\AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState'
New-Item -ItemType Directory -Path $defaultDestPath -Force | Out-Null
Copy-Item -Path $Source -Destination $defaultDestPath -Force

# 处理所有现有用户的Start Menu目录
$userDestTemplate = 'AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState'
Get-ChildItem 'C:\Users' -Directory | ForEach-Object {
    $destPath = Join-Path $_.FullName $userDestTemplate
    New-Item -ItemType Directory -Path $destPath -Force | Out-Null
    Copy-Item -Path $Source -Destination $destPath -Force
}

内容的提问来源于stack exchange,提问作者Bram

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 19:35:22