1

Very new to Powershell, so I'm not sure I'm doing things the right way at the moment, but I'm struggling pass a variable into an Invoke-Expression string and was hoping someone would be able to help.

$projectLocation = "C:/Users/Admin/Desktop/Projects/repo"

Invoke-Expression 'cmd /c start powershell -Command {
     cd $projectLocation;
     git pull
}'

I've also tried splitting out the string with ' + $projectLocation + ', but still not having much luck.

The only way I've achieved my goal is by inserting the $projectLocation string manually.

I don't think I'm far off, but any help would be great! Thanks.

4
  • 1
    Why don't you launch powershell directly or use Start-Process from within powershell? Invoke-Expression seems wholy unnecessary here. What is the actual goal you're trying to achieve here? Commented Jul 19, 2019 at 8:42
  • @MathiasR.Jessen do you have an example? I'm currently learning as I'm going so don't really understand the ins and outs of what powershell is and isn't capable of right now. My goal here is to create a "front-end" for non-technical users to git checkout master/branches and pull so they don't have to worry about remembering commands etc. Commented Jul 19, 2019 at 8:49
  • @Olaf that worked as expected, thank you! I'll stick with that for now, but it seems like there's probably a "better" way of achieving this that I'll look into too. Thanks a lot :) Commented Jul 19, 2019 at 8:53
  • @Nick: mathias answer is exactly what the way you should be using in production. Clean and neat way. You do not require to use Invoke-Expression Commented Jul 19, 2019 at 8:54

1 Answer 1

3

Evaluating a piece of code that spawns cmd in order to launch a new instance of powershell.exe is completely unnecessary:

$projectLocation = "C:/Users/Admin/Desktop/Projects/repo"
cd $projectLocation
git pull

If you want to suppress any output it might generate, just wrap it in a block or function and redirect the output streams to $null:

function Pull-Repo
{
    param(
      [string]
      $projectLocation = "C:/Users/Admin/Desktop/Projects/repo"
    )

    Push-Location $projectLocation
    git pull
    Pop-Location
}

# suppress all output
Pull-Repo *> $null
Sign up to request clarification or add additional context in comments.

4 Comments

The reason I'm re-opening powershell in this instance it to keep my "dashboard" for lack of a better term clean. So the user can just use that to input their options. I appreciate that my code example doesn't really give a picture of what I'm trying to achieve though!
Here's an image of my "front-end" So for example, submitting "2" would git checkout master and git pull. (Separate powershell instance" ibb.co/VThV7Fc I've just seen your update. That looks exactly like what I'm after :)
Thanks a lot for your help Mathias, really appreciate your time!
Cool, happy to help!

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.