Powershell – Deploying a desktop link to all users on multiple computers

Powershell can easily deploy any kind of desktop link, with basically all the options! You can specify an exact icon, arguments, description, window style, and more!!!

That’s pretty neat.

# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
   {
   # We are running "as Administrator" - so change the title and background color to indicate this
   $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
   $Host.UI.RawUI.BackgroundColor = "DarkBlue"
   clear-host
   }
else
   {
   # We are not running "as Administrator" - so relaunch as administrator
   # Create a new process object that starts PowerShell
    $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
   # Specify the current script path and name as a parameter
   $newProcess.Arguments = $myInvocation.MyCommand.Definition;
   # Indicate that the process should be elevated
   $newProcess.Verb = "runas";
   # Start the new process
   [System.Diagnostics.Process]::Start($newProcess);
   # Exit from the current, unelevated, process
   exit
   }


write-host "##########################################################################" -ForegroundColor Green
write-host "#                                                                        #" -ForegroundColor Green
write-host "#         This will distribute a desktop icon to all workstations        #" -ForegroundColor Green
write-host "#            that are listed in your `"workstations.txt`" file.            #" -ForegroundColor Green
write-host "#                                                                        #" -ForegroundColor Green
write-host "##########################################################################" -ForegroundColor Green
write-host ""

#--- VARIABLES
    #--- TXT FILE CONTAINING A LIST OF WORKSTATION IPs (hostnames not recommmended due to DNS issues)
    $workstations = "$env:WORKINGPATH\setup\config_files\workstations.txt"

#--- TEST ALL WORKSTATIONS
(Get-Content $workstations) -notmatch '^#' | foreach {
    if (test-Connection -Count 1 -Cn $_ -quiet) {
        Write-Host "$_ is online" -ForegroundColor Green
    } else {
        Write-Host "$_ is not online" -ForegroundColor Red
		   }
		   }

#--- Pause Script 3 2 1 GO!
$x = 3
$length = $x / 100
$MinText = "minutes"
Write-Host ""
while($x -gt 0) {
$min = [int](([string]($x/60)).split('.')[0])
if ($min -eq "1"){$MinText = "minute"}
if ($min -eq "0") {
$min = $null
$MinText = "less than a minute"
$Beep = [system.console]::Beep(2000,150)
}

$text = $min + ($x % 60)
#Write-Progress "Pausing Script" -status $text -perc ($x/$length)
Write-Host "$text..." -NoNewline
$beep
start-sleep -s 1
$x--
}
Write-Host "GO!!" -ForegroundColor Green
$Beep = [system.console]::Beep(3300,500)


#--- GET LIST AND TEST CONNECTION
(Get-Content $workstations) -notmatch '^#' | foreach {
    if (test-Connection -Count 1 -Cn $_ -quiet) {

        #--- SET VARIABLE
        Write-Host ""
        Write-Host "Saving desktop link for $_" -ForegroundColor Green
        $TargetFile = "C:\Windows\System32\mshta.exe"
        $ShortcutFile = "\\$_\c$\Users\Public\Desktop\App Setup.lnk"
        $IconFile = "%WORKINGPATH%\icons\app-setup.ico"
        $WScriptShell = New-Object -ComObject WScript.Shell
        $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
        $Shortcut.TargetPath = $TargetFile
        $Shortcut.IconLocation = $IconFile
        $Shortcut.Arguments = "%WORKINGPATH%\setup\App-Setup-Client.hta"
        $Shortcut.Save()

    } else {
        "$_ is not online"
		   }
		   }

write-host "All Done!!!"


#--- ALL DONE POPUP
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Finished adding setup links to client desktops!!",0,"All Done!",0x0)

Leave a Reply

Your email address will not be published. Required fields are marked *