Home > OS >  Setting up Git Server: Why is access denied while setting variables using Powershell
Setting up Git Server: Why is access denied while setting variables using Powershell

Time:03-31

I'm trying to set up a local Git server on Windows the way it is described on this website: https://github.com/PowerShell/Win32-OpenSSH/wiki/Setting-up-a-Git-server-on-Windows-using-Git-for-Windows-and-Win32_OpenSSH. When I try to set the variable $machinePath ($machinePath = ${C:\Program Files\Git\mingw64\bin}::GetEnvironmentVariable('Path', 'MACHINE')) I get an error message telling me that accessing the path C:\Program Files\Git\mingw64\bin was denied. I do run PowerShell as Administrator. Can anyone tell me how to fix that?

CodePudding user response:

The first step of your linked guide says to run these commands to add git to your PATH globally. There's no reason to change them unless you installed git somewhere else:

$gitPath = Join-Path -Path $env:ProgramFiles -ChildPath "git\mingw64\bin"
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'MACHINE')
[Environment]::SetEnvironmentVariable('Path', "$gitPath;$machinePath", 'Machine')

Note that setting machine-level environment variables does require running-as-administrator.


As for powershell syntax:

# accessing a class's member method/property is done via 
[ClassName]::Method('parameter1','p2')

# curly brackets are usually script blocks or hash tables
$sciptblock = { ping localhost }
$hashtable = @{ key1 = 'value1'; key2 = 'value2'}

# they can also be used to set a variable name with spaces:
${a b c} = 'abc'

# BUT if you have a file path, powershell will GET/SET the data of the file:
${c:\temp\test.txt} = 'test file'
  • Related