Powershell script not writing shortcut

Antonio Santiago 21 Reputation points
2022-06-17T19:52:59.237+00:00

I have the script bellow that is supposed to look for all user folders, then locate M365 apps shortcuts and update the target path from x86 location to x64, however, even when the script does not return any errors, the shortcuts are never updated. Could anyone help me getting this figured out please?

=========================================================================================================================

$wshShell = New-Object -ComObject WScript.Shell
$userProfiles = @()
$officeShortcuts = @()
$officeShortcutNames = @("Word", "Outlook", "Excel", "PowerPoint", "Publisher", "Access", "OneNote", "Visio", "Project")

$userProfiles = Get-ChildItem -Exclude "lsweeper" "C:\Users"

foreach ($profile in $userProfiles) {
#Build required user profile paths
$requiredPaths = @("$profile\Desktop", "$profile\OneDrive - Tideworks Technology\Desktop", "$profile\AppData\Roaming\Microsoft\Windows\Start Menu\Programs")

foreach ($path in $requiredPaths) {
#Find existing Office shortcuts
if (Test-Path $path) {
#The path exists
$officeShortcuts += Get-ChildItem -Path $path -Filter "*.lnk" | Where-Object { $_.BaseName -in $officeShortcutNames }
}
}
}

foreach ($sc in $officeShortcuts) {
#Inspect each shortcut (target path)
$AppName = $sc
$bs = $path + "\"
$Newscname = $bs + $sc
$newsc = $WshShell.CreateShortcut($Newscname)
$Basename = ([IO.FileInfo] $sc.TargetPath).BaseName
$NewPath = "C:\Program Files\Microsoft Office\root\Office16\" + $AppName
$newsc.TargetPath = $NewPath
$Shortcut.Save()
}

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,627 questions
No comments
{count} votes

Accepted answer
  1. MotoX80 23,651 Reputation points
    2022-06-18T02:11:01.897+00:00

    You are not processing $officeShortcuts correctly. Try the script below. Note that I do not have Office installed to be able to fully test this.

    $wshShell = New-Object -ComObject WScript.Shell  
    $userProfiles = @()  
    $officeShortcuts = @()  
    $officeShortcutNames = @("Word", "Outlook", "Excel", "PowerPoint", "Publisher", "Access", "OneNote", "Visio", "Project")  
      
    $userProfiles = Get-ChildItem -Exclude "lsweeper" "C:\Users"  
      
    foreach ($profile in $userProfiles) {  
        #Build required user profile paths  
        $requiredPaths = @("$profile\Desktop", "$profile\OneDrive - Tideworks Technology\Desktop", "$profile\AppData\Roaming\Microsoft\Windows\Start Menu\Programs")  
      
        foreach ($path in $requiredPaths) {  
            #Find existing Office shortcuts  
            if (Test-Path $path) {  
                #The path exists  
                $officeShortcuts += Get-ChildItem -Path $path -Filter "*.lnk" | Where-Object { $_.BaseName -in $officeShortcutNames }  
            }  
        }  
    }  
      
    foreach ($sc in $officeShortcuts) {         # $officeShortcuts contains file objects   
        #Inspect each shortcut (target path)  
        "Processing {0}" -f $sc.FullName  
        remove-item -path $sc.FullName          # delete existing shortcut    
        $AppName = $sc.basename                 # get the name ex: Word   
        $newsc = $WshShell.CreateShortcut($sc.fullname)             
        $NewPath = "C:\Program Files\Microsoft Office\root\Office16\" + $AppName + ".exe"  
        "Setting it to execute {0}" -f $NewPath  
        $newsc.TargetPath = $NewPath  
        $newsc.Save()      
    }   
          
    

0 additional answers

Sort by: Most helpful