We help IT Professionals succeed at work.

"Double-Click" an icon on the desktop with a Script.

jskfan
jskfan asked
on
"Double-Click" an icon on the desktop with a Script.

I have this script below. That replaces a string in a file on each computer listed on the CSV file.
after the string has been replaced on a computer,  I would like it  to execute(Double-Click) on an icon located on the Desktop  , lets say the name of the Icon is "DoubleClickhere"

Thank you

## csv file with machines and user names to process:
$csv = 'c:\Scripts\Computers.csv'
## Absolute local file path on the clients:
$filePath = 'C:\test.txt'

$pattern = '(-username\s+)\S+'
Import-Csv -Path $csv | ForEach-Object {
	$computer = $_.ComputerName.Trim()
	$newUser = $_.NewUsername.Trim()
	$fileUNC = "\\$($computer)\$($filePath.Replace(':', '$'))"
	Write-Host "Processing $($computer), new user '$($newUser)' ..." -NoNewline
	Try {
		$content = Get-Content -Path $fileUNC -ErrorAction Stop
		$content -replace $pattern, "`$1$($newUser)" | Set-Content -Path $fileUNC -Force -ErrorAction Stop
		Write-Host " OK" -ForegroundColor Green
	} Catch {
		Write-Host " FAILED: $($_.Exception.Message)" -ForegroundColor Red
	}
}

Open in new window

Comment
Watch Question

End-user support
BRONZE EXPERT
Commented:
Add below this line...
Write-Host " OK" -ForegroundColor Green

Open in new window

...so it becomes:
Write-Host " OK" -ForegroundColor Green
# Save user's Desktop folder path
$DesktopPath = [Environment]::GetFolderPath("Desktop")
# Run the user's Desktop shortcut
invoke-item "$DesktopPath\DoubleClickhere.lnk"

Open in new window

Author

Commented:
NVIT

Thank you.
Can you comment each line  of the code what it does ?
David Johnson, CDSimple Geek from the '70s
GOLD EXPERT
Distinguished Expert 2019

Commented:
the code is already commented

Author

Commented:
Perfect..it works fine on one computer

can you make it read computers from CSV and execute code for each computer
example from 'c:\Scripts\Computers.csv'

Author

Commented:
any updates on this ?
Thank you
David Johnson, CDSimple Geek from the '70s
GOLD EXPERT
Distinguished Expert 2019

Commented:
Invoke-Command -ComputerName Server01 -Credential Domain01\User01 -ScriptBlock { Get-Culture }

Open in new window


What happens .. you already have the list of computers and users?
note any output-host won't work as it outputs to the active computers screen not the screen where it is called from

Author

Commented:
Yes I have CSV file that contains computer names.
I can make it text file if that's easier for you
NVITEnd-user support
BRONZE EXPERT

Commented:
The original question asks to help with: "Double-Click" an icon on the desktop with a Script, which I have answered.

Author

Commented:
Thanks INVIT


AUTHOR Commented: 2019-12-15
Perfect..it works fine on one computer

can you make it read computers from CSV and execute code for each computer
example from 'c:\Scripts\Computers.csv'
Qlemo"Batchelor", Developer and EE Topic Advisor
SILVER EXPERT
Top Expert 2015

Commented:
To simulate clicking on a desktop icon you need to log in remotely as the respective user. Quite difficult if that user happens to matter for "executing" the shortcut. Does it matter?
Is the shortcut target some kind of setup only ran for changing the user data stored in the changed file?

Author

Commented:
it is always  the the Administrator account who logs in ... it is one time double-click by the Administrator account...and that is it
I need the script to "double-click" on an icon on each computer desktop( listed on csv file).
the double-click is triggered with Administrator account.

Thank you

Author

Commented:
Thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.