1

I am wondering why one method works but the other does not

Not sure what to try

1.

$syspinEXE = "Z:\syspin.exe"
$programDataPath = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs"
&$syspinEXE "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Word 2016.lnk" 5386


2.

$syspinEXE = "Z:\syspin.exe"
$programDataPath = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs"
&$syspinEXE "$programDataPath","Word 2016.lnk" -join "\" 5386

I am wondering why the first snippet works but the second snippet does not. I believe they should both work, but this is obviously not true.

5
  • Could you please remove the double quotes from "$programDataPath" and try again? :) Commented Aug 12, 2019 at 13:51
  • 3
    Put "$programDataPath","Word 2016.lnk" -join "\" in parentheses, otherwise -join and "\" will be interpreted as a parameter name and a separate argument to & Commented Aug 12, 2019 at 13:55
  • @MathiasR.Jessen Thank you so much! Your solution worked. I am new to scripting with PowerShell and am doing my best to learn. Would you mind explaining why parentheses works here? I am not sure how they effect the program flow. Preliminary thanks for your explanation. Commented Aug 12, 2019 at 14:26
  • 2
    The ( ) causes the expression within them to be evaluated first before being passed to the command. (Otherwise, as noted, it will be seen as separate parameters.) Commented Aug 12, 2019 at 14:30
  • 2
    @Fennec: The double quotes are certainly not necessary, but removing them makes no difference here. Commented Aug 12, 2019 at 14:58

1 Answer 1

1

Suggestion:

$startMenuPath = [Environment]::GetFolderPath([Environment+SpecialFolder]::CommonStartMenu)
Get-ChildItem $startMenuPath -Filter "Word 2*" -Recurse |
  Select-Object -First 1 | ForEach-Object {
    & $_.FullName 5386
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.