powershellelevated-privilegesshortcut-file

How do I write a script to copy an item to the public desktop?


I am trying to write a script to push through datto to copy a shortcut to each endpoint's public desktop. UAC is getting in my way, and I'm trying to find a way around it.

So far I have tried this script which fails due to not having admin perms:

Function ShortcutCopy {
        Copy-Item -Path "\\path\to\file" -Destination "C:\Users\Public\Desktop"
        }

ShortcutCopy

Then I modified it to try to get it to run powershell as admin and run the command from the new instance, but it opens elevated powershell then immediately closes without actually copying the shortcut:

Function ShortcutCopy {
        Copy-Item -Path "\\path\to\file" -Destination "C:\Users\Public\Desktop"
        }

powershell -Command "Start-Process powershell -Verb RunAs -ArgumentList 'ShortcutCopy'"

The shortcut is located in a network share that is accessible. Is there a better way to write this?


Solution

  • Therefore:

    Start-Process powershell.exe -Verb RunAs -ArgumentList @'
      "Function ShortcutCopy {
        Copy-Item -Path \"\\path\to\file\" -Destination \"C:\Users\Public\Desktop\"
      }
      ShortcutCopy"
    '@