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.
Post Reply
User avatar
mqh777
Posts: 229
Joined: Tue Feb 25, 2014 1:01 pm

desktop shortcuts

Post by mqh777 » Tue Oct 16, 2018 8:13 am

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!'
	}

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: desktop shortcuts

Post by jvierra » Tue Oct 16, 2018 8:20 am

This "$GetUserName" is undefined.

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: desktop shortcuts

Post by jvierra » Tue Oct 16, 2018 8:24 am

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
mqh777
Posts: 229
Joined: Tue Feb 25, 2014 1:01 pm

Re: desktop shortcuts

Post by mqh777 » Tue Oct 16, 2018 8:28 am

I had tried using \\C$ and that returns nothing. No user or public shortcuts get returned.

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: desktop shortcuts

Post by jvierra » Tue Oct 16, 2018 8:33 am

Sorry. I forgot the computer name. I fixed the example.

User avatar
mqh777
Posts: 229
Joined: Tue Feb 25, 2014 1:01 pm

Re: desktop shortcuts

Post by mqh777 » Tue Oct 16, 2018 8:42 am

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.

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: desktop shortcuts

Post by jvierra » Tue Oct 16, 2018 11:21 am

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
mqh777
Posts: 229
Joined: Tue Feb 25, 2014 1:01 pm

Re: desktop shortcuts

Post by mqh777 » Tue Oct 16, 2018 12:44 pm

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?

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: desktop shortcuts

Post by jvierra » Tue Oct 16, 2018 1:37 pm

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.

Post Reply