Problem with Double Dash -- in variable

I am having a problem when a double dash (–) is in a variable. In the grand scheme I want to have a variable that can handle any git option or combo of git options a user needs. The problem is when the variable is used to create the git command. If I manually put in --depth 1 instead of $CommandLineOptions the git command works fine in Powershell. But when I use the variable I get the error below. I have tried using `-- and `-`-. I have also tried using other ways to run the git command like invoke-command etc… I know I’m missing something here with powershell and variables but I don’t know what.

Any help would be appreciated.

$RepoUri = "YourFavoritURL.git"

$CommandLineOptions = "--depth 1"

$GitCommand = "git clone $CommandLineOptions $RepoUri"
write-output $GitCommand

git clone $CommandLineOptions $RepoUri

PS F:\git-clone> .\doubledash.ps1
git clone --depth 1 YourFavoritURL.git
error: unknown option `depth 1’
usage: git clone [] [–] []

-v, --verbose         be more verbose
-q, --quiet           be more quiet
--progress            force progress reporting
-n, --no-checkout     don't create a checkout
--bare                create a bare repository
--mirror              create a mirror repository (implies bare)
-l, --local           to clone from a local repository
--no-hardlinks        don't use local hardlinks, always copy
-s, --shared          setup as shared repository
--recursive           initialize submodules in the clone
--recurse-submodules  initialize submodules in the clone
-j, --jobs         number of submodules cloned in parallel
                      directory from which templates will be used
--reference     reference repository
--dissociate          use --reference only while cloning
-o, --origin    use  instead of 'origin' to track upstream
-b, --branch 
                      checkout  instead of the remote's HEAD
-u, --upload-pack 
                      path to git-upload-pack on the remote
--depth        create a shallow clone of that depth
--single-branch       clone only one branch, HEAD or --branch
--shallow-submodules  any cloned submodules will be shallow
                      separate git dir from working tree
-c, --config 
                      set config inside the new repository
-4, --ipv4            use IPv4 addresses only
-6, --ipv6            use IPv6 addresses only</em>

You might try using Invoke-Expression to run $GitCommand; the problem here isn’t PowerShell per se. It’s that PowerShell has to run Cmd.exe, and it tries to make some guesses about how to do that, and it’s guessing wrong. See if Invoke-Expression doesn’t help.

Or try something like the below, when there is a need to call cmd.exe commands.
It, of course starts the PowerShell consolehost in a no exit sate to show results.

I’ve used to do this inline, but converted it to a small function with an alias to
make this a bit more convenient whenever, I needed to pop out to cmd.exe stuff
for whatever reason.

Function Start-ConsoleCommand

Param  ( [string]$ConsoleCommand )

Start-Process "$PSHOME\powershell.exe" -ArgumentList "-NoExit","-Command  &amp;{ $ConsoleCommand }" 

Set-Alias scc Start-ConsoleCommand

scc -ConsoleCommand “git clone $CommandLineOptions $RepoUri”

Now, I’ve never tried this with anything which requires the double dash thingy.
I don’t use git, and have no other tools which use dash dash as a thingy.

So, I installed the get client and running the above using the PowerShell_ISE
against your sample produces …

fatal: repository ‘YourFavoritURL.git’ does not exist
PS D:\Scripts>

… and not the git help / error response you are getting

Turning you sample into a script and doing the below…

scc -ConsoleCommand “git clone D:\Scripts\GitUsingDoubleDashSwitch.ps1”

… in the ISE and console host produces …

Cloning into ‘GitUsingDoubleDashSwitch.ps1’…
fatal: Invalid gitfile format: D:\Scripts\GitUsingDoubleDashSwitch.ps1
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
PS D:\Scripts>

Thank you for the suggestion of Invoke-Expression. The script is working perfectly now. I thought I had already tried this, but looking back I had only tried invoke-command.

Thank you for the suggestion. I am going to save this for future reference!

Moving to correct forum.