首页 > 解决方案 > 移动桌面项目

问题描述

我的代码需要一些帮助。我目前在 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

在此处输入图像描述

标签: powershellpowershell-3.0

解决方案


我认为您将事情与批处理文件混为一谈。这是我的方法:

#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 实际执行的操作(而不是仅执行此操作)。如果你得到的输出对你来说没问题,你只需要删除这个参数来执行任务。


推荐阅读