desktop shortcuts

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

desktop shortcuts

Post by mqh77777 »

We need to get desktop shortcuts on remote systems. The code below works for the Public desktop but not for the users desktop. But if I run this in PowerShell ISE then it works for both. What did I do wrong?

Code: Select all

$buttonGetDesktopShortcuts_Click = {
	$statusbar1.text = 'Find desktop shortcuts, please wait...'
	$richtextbox_output.Clear()
	$richtextbox_output.SelectionColor = 'Blue'
	$Results = Invoke-Command -ComputerName $PCNameBox.Text -ScriptBlock {
	$UserDesktop = (Get-ChildItem -Path "C:\Users\$GetUserName.Text\Desktop").name |  Sort-Object
	$PublicDesktop = (Get-ChildItem -Path "C:\Users\Public\Desktop").name  | Sort-Object
	$UserDesktop + $PublicDesktop | Out-String
	}  
	 
	$richtextbox_output.AppendText($results)
	$statusbar1.text = 'All done!'
	}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: desktop shortcuts

Post by jvierra »

This "$GetUserName" is undefined.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: desktop shortcuts

Post by jvierra »

There is really no need to remote. Just do this:

Code: Select all

	$UserDesktop = (Get-ChildItem -Path "\\$computer\C$\Users\$($GetUserName.Text)\Desktop").name |  Sort-Object
	$PublicDesktop = (Get-ChildItem -Path "\\$computer\C$\Users\Public\Desktop").name  | Sort-Object
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: desktop shortcuts

Post by mqh77777 »

I had tried using \\C$ and that returns nothing. No user or public shortcuts get returned.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: desktop shortcuts

Post by jvierra »

Sorry. I forgot the computer name. I fixed the example.
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: desktop shortcuts

Post by mqh77777 »

I have tried both:

Code: Select all


$UserDesktop = (Get-ChildItem -Path "\\$computer\C$\Users\$($GetUserName.Text)\Desktop").name |  Sort-Object
$PublicDesktop = (Get-ChildItem -Path '\\$computer\C$\Users\Public\Desktop').name  | Sort-Object

$UserDesktop = (Get-ChildItem -Path "\\$PCNameBox.Text\C$\Users\$($GetUserName.Text)\Desktop").name |  Sort-Object
$PublicDesktop = (Get-ChildItem -Path '\\$PCNameBox.Text\C$\Users\Public\Desktop').name  | Sort-Object
I have also tried both with single and double quotes.

Neither one returns anything.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: desktop shortcuts

Post by jvierra »

Did you forget to assign a computer name to "$computer"?
Did you forget to use "expression evaluator" on the control property? - Yes I don't see it.
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: desktop shortcuts

Post by mqh77777 »

Hi, you wrote:
Did you forget to assign a computer name to "$computer"? Yes it does have a value. This is part of a PowerShell Studio form.
Did you forget to use "expression evaluator" on the control property? - Yes I don't see it. what is the expression evaluator?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: desktop shortcuts

Post by jvierra »

It is formally referred to as the "subexpression operator".

https://ss64.com/ps/syntax-operators.html
It allow an expression to be forcibly evaluated in an expandable string. Look at my original code to see where I used it.

"dot" referenced object properties do not expand in a string. They must be forced to evaluate using the subexpression operator.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked