forked from djust270/Intune-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveDuplicatedShortcuts_plustask.ps1
36 lines (31 loc) · 2.01 KB
/
RemoveDuplicatedShortcuts_plustask.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#If you are enforcing One Drive Known File Move, redirecting the Desktop, its possible some shortcuts may be duplicated
#between the time the computer enrolls in intune and one drive redirects the desktop. This script will create a
#scheduled task to clear up and remove duplicated shortcuts if they exist at logon.
#Just add in the shortcut name and your companies One Drive path.
$dupe = '$UserSID = (New-Object -ComObject Microsoft.DiskQuota).TranslateLogonNameToSID((Get-WmiObject -Class Win32_ComputerSystem).Username)
$Path = "HKLM\SOFTWARE\Microsoft\WIndows NT\CurrentVersion\Profilelist\$UserSID"
$UserPath = Get-ItemProperty "Registry::$path" -name "ProfileImagePath" | select -ExpandProperty ProfileImagePath
$duplicateshortcuts = "shortcut1.lnk","shortcut2.lnk","shortcut3.lnk" | foreach {test-path "$UserPath\OneDrive - YourCompany\Desktop\$_."}
if ($duplicateshortcuts -eq $true)
{
$OldShortcuts = @("$UserPath\OneDrive - YourCompany\Desktop\shortcut1.lnk","$UserPath\OneDrive - YourCompany\Desktop\shortcut1.lnk.lnk","$UserPath\OneDrive - YourCompany\Desktop\shortcut3.lnk","$UserPath\OneDrive - YourCompany\Desktop\Microsoft Edge - Copy.lnk")
Get-ChildItem $OldShortcuts | Remove-Item
}'
new-item c:\automation -ItemType directory -force
set-content c:\automation\removedupshortcut.ps1 $dupe
if( -Not (Get-ScheduledTask -TaskName "Remove Duplicate Shortcuts" -ErrorAction SilentlyContinue -OutVariable task) )
{
$Params = @{
Action = (New-ScheduledTaskAction -Execute 'powershell' -Argument '-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass C:\Automation\removedupshortcut.ps1')
Trigger = (New-ScheduledTaskTrigger -AtLogOn)
Principal = (New-ScheduledTaskPrincipal -GroupId "System")
TaskName = 'Remove Duplicate Shortcuts'
Description = 'Remove Duplicate Shortcuts'
}
Register-ScheduledTask @Params
Start-ScheduledTask -TaskName "Remove Duplicate Shortcuts"
}
else
{
Start-ScheduledTask -TaskName "Remove Duplicate Shortcuts"
}