powershell - 移动桌面项目
问题描述
我的代码需要一些帮助。我目前在 Citrix 环境中工作,并在第 4 行不断地碰壁。我已经尝试过创建和不创建名为 desktopicons 的变量。
当我运行前 3 行时,似乎所有代码都运行良好,直到第四行。
PS 脚本的主要思想是创建一个新文件夹,将所有桌面图标移动到这个新创建的文件夹中。
mkdir -Name "newfolder" -Path "C:\Users\%username%\Desktop\" -Force
Get-Process "C:\Users\%username%\Desktop\*" | Foreach-Object { $_.CloseMainWindow() | Out-Null } | stop-process
$desktopicons="$Env: C:\Users\%username%\Desktop"
Move-Item -Exclude "$desktopicons\newfolder" -Path "$desktopicons*" -Destination "$desktopicons\newfolder\" -Force
解决方案
我认为您将事情与批处理文件混为一谈。这是我的方法:
#get desktop path for current user
$DesktopPath = $ENV:HOMEDRIVE+$ENV:HOMEPATH+"\Desktop"
#create "newfolder" on users desktop
New-Item -Path $DesktopPath -Name 'newfolder' -Type Directory
#move all files from desktop into "newfolder" excluding "newfolder"
Move-Item -Path $DesktopPath"\*" -Destination $DesktopPath"\newfolder" -exclude 'newfolder' -WhatIf
我添加了参数-WhatIf
以查看 line 实际执行的操作(而不是仅执行此操作)。如果你得到的输出对你来说没问题,你只需要删除这个参数来执行任务。
推荐阅读
- matlab - 如何检索状态块的句柄?MATLAB
- mysql - 使用 docker-compose 按顺序注入 .sql 文件
- c# - 如何验证需要传递到硬编码路径以运行 File.Exists 的子文件夹(名称会有所不同)
- excel - 如何获取网站生成的数据到excel
- wordpress - 无法在 WordPress 中运行 cron 作业
- android-livedata - 如何使用 liveData 协程块
- ionic-framework - Ionic 应用程序在启动画面后卡在白屏上
- scala - 比较 Seq 和 Array 的不同行为
- javascript - 如何在 jquery 数据表中显示没有(键值)对的数据?
- c# - 为什么我的应用程序在使用 webClient 上传图片后变慢了?