Windows Path 다루기

  • #powershell

PowerShell로 Windows PATH 환경 변수 관리하기 - 번역

  • 원본 링크 : Path 다루기, TechTarget
    • 콘텐츠 보는데 회사 이메일 넣으라고 나온다. 일단 번역해놓자
  • Anthony Howell, 18 Oct 2019
  • Windows PATH 환경 변수는 application들이 실행파일을 찾을때 사용한다
    • system이나 utility의 설치를 되게할수도 깰수도 있다.
  • 관리자는 PowerShell을 이용해 문자열 조작을 포함하는 PATH 변수 관리를 할 수있다
  • PATH 변수 접근법
    1
    
    > $env:path
    
  • 좀더 가독성을 높이려면..
    1
    
    > $env:path -split ';'
    

PATH 변수에 경로 추가하기

  • PATH 변수에 추가하려면, 경로 맨 뒤에 semicolon과 신규 경로를 문자열로 붙이면 된다
  • PowerShell로 우리가 추가하려는 경로가 이미 PATH 상에 존재하는지 확인할 수있다
  • 우선 추가하려는 경로를 pick한다
    1
    
    > $addPath = 'c:\TopSecret\Bin`
    
  • 경로 문자열은 맨 끝 slash가 있든 없든 상관없으며 경로는 정확하게 resolve 될것이다
  • PATH 변수에 신규 경로를 덧붙이기전에, 해당 경로가 이미 있는지 체크해야한다
    • 이렇게 하기위해 마지막에 slash가 있을때 없을때 모두에 대해 경로 유무를 체크해야한다
      1
      2
      
      $regexAddpath = [regex]::Escape($addPath)
      $arrPath = $env:Path -split ';' | Where-Object {$_ -notMatch "^$regexAddPath\\​\\?"}
      

      Since we are using regular expressions to check for a trailing ‘', escape the $addPath, as it will likely have slashes in it.

After we have our array of paths that doesn’t include our new one, we can add our path to it, join them with the semicolon and assign that to the PATH variable:

1
$env:Path = ($arrPath + $addPath) -join ';'

Then, collect that array together as a single function:

1
2
3
4
5
6
7
8
9
10
11
12
Function Add-PathVariable {
    param (
        [string]$addPath
    )
    if (Test-Path $addPath){
        $regexAddPath = [regex]::Escape($addPath)
        $arrPath = $env:Path -split ';' | Where-Object {$_ -notMatch "^$regexAddPath\\​\\?"}
        $env:Path = ($arrPath + $addPath) -join ';'
    } else {
        Throw "'$addPath' is not a valid path."
    }
}

How to remove a path

It’s just as simple to remove a path from the Windows PATH environment variable as it is to add one. For this example, circle back to Figure 2, which shows some of the paths on the computer, and remove Java:

1
2
$removePath = 'C:\Program Files (x86)\Common
Files\Oracle\Java\javapath'

Again, split the PATH and only select the paths that don’t match the $removePath with a possible trailing slash. This time, however, we will simply assign that output back to the PATH variable:

1
2
3
$regexRemovePath = [regex]::Escape($removePath)
$arrPath = $env:Path -split ';' | Where-Object {$_ -notMatch "^$regexRemovePath\\?"}
$env:Path = $arrPath -join ';'

Because this process is so similar to adding a path, we can combine these processes into one function rather than have two:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Set-PathVariable {
    param (
        [string]$AddPath,
        [string]$RemovePath
    )
    $regexPaths = @()
    if ($PSBoundParameters.Keys -contains 'AddPath'){
        $regexPaths += [regex]::Escape($AddPath)
    }

    if ($PSBoundParameters.Keys -contains 'RemovePath'){
        $regexPaths += [regex]::Escape($RemovePath)
    }
    
    $arrPath = $env:Path -split ';'
    foreach ($path in $regexPaths) {
        $arrPath = $arrPath | Where-Object {$_ -notMatch "^$path\\?"}
    }
    $env:Path = ($arrPath + $addPath) -join ';'
}

Editor’s note: This expert answer is the final part in a three-part series on PowerShell automation. See another expert answer on ACL folder management, and one on Windows event log monitoring.

fenced code block내에 double backslash를 어떻게 넣는가?

minpac 사용법 (2022-02-15)

다크 모드 UI 자료

구글 폰트의 Effect (2022-02-17)