Post

The helpful PowerShell Functions and Commands

Useful PowerShell functions and commands for managing services, processes, and Java environment on Windows.

The helpful PowerShell Functions and Commands

1. Check Status and Stop the Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function Stop-Service {
    Param (
        [ValidateSet("TCP", "UDP")] $Method,
        $Port
    )

    $Process = Get-Process -Method $Method -Port $Port

    if ($Process) {
        Stop-Process -Id $Process.OwningProcess -Force
        Write-Host "Process with PID $($Process.OwningProcess) has been terminated."
    } else {
        Write-Host -f Red "Process is not found on port $Port."
    }
}

function Show-Status {
    Param (
        [ValidateSet("TCP", "UDP")] $Method,
        $Port
    )

    $Process = Get-Process -Method $Method -Port $Port

    if ($Process) {
        Write-Host -f Green "Has a process with PID $($Process.OwningProcess) on port $Port."
    } else {
        Write-Host -f Red "No process found on port $Port."
    }
}

function Get-Process {
    Param (
        [ValidateSet("TCP", "UDP")] $Method,
        $Port
    )

    if ($Method -eq "TCP") {
        return Test-TCP-Status $Port
    } elseif ($Method -eq "UDP") {
        return Test-UDP-Status $Port
    }
}

function Test-TCP-Status {
    Param ($Port)
    return Get-NetTCPConnection -State Listen | Where-Object { $_.LocalPort -eq $Port } | Select-Object -First 1
}

function Test-UDP-Status {
    Param ($Port)
    return Get-NetUDPEndpoint | Where-Object { $_.LocalPort -eq $Port } | Select-Object -First 1
}

How to use:

1
2
3
4
5
6
7
8
9
10
function test {
    $Process = Get-Process -Method TCP -Port 5050
    if ($Process) {
        Write-Host -f Green "Process with PID $($Process.OwningProcess) is running."
        Stop-Service -Method TCP -Port 5050
        Exit
    }
}

test

2. Waiting for the service to run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function Wait-For-Starting {
    Param (
        [ValidateSet("TCP", "UDP")] $Method,
        $Port
    )
    $i = 0
    if ($Method -eq "TCP") {
        while (-not (Test-TCP-Status $Port)) {
            $RunningText = "Starting" + ("." * ($i % 3))
            Write-Progress -Activity $RunningText -Status "Take ${i}s"
            Start-Sleep -Milliseconds 1000

            $i++

            if($i-eq "15") { # timeout after 15s
                Write-Host -f Red "Process can not run on port $Port."
                Exit
            }
        }
    } elseif ($Method -eq "UDP") {
        while (-not (Test-UDP-Status $Port)) {
            $RunningText = "Starting" + ("." * ($i % 3))
            Write-Progress -Activity $RunningText -Status "Take ${i}s"
            Start-Sleep -Milliseconds 1000

            $i++

            if($i-eq "15") { # timeout after 15s
                Write-Host -f Red "Process can not run on port $Port."
                Exit
            }
        }
    }
}

How to use:

1
Wait-For-Starting -Method TCP -Port 5050

*Note: Refer to Test-TCP-Status and Test-UDP-Status Function from #1

3. Check if the command exists

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function Test-Command {
    Param ($Command)
    $oldPreference = $ErrorActionPreference
    $ErrorActionPreference = 'stop'
    try {
        if (Get-Command $Command) {
            return $True
        }
    }
    catch {
        return $False
    }
    finally {
        $ErrorActionPreference = $oldPreference
    }
}

# how to use
Write-Host (Test-Command java)

4. Get/set ENV

1
2
3
4
5
6
7
8
9
# Get the current value of the PATH variable
$CurrentPath = [System.Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::User)
$NewPath = $CurrentPath + ";" + $Path

# Set the New Path to User ENV
[System.Environment]::SetEnvironmentVariable("PATH", $NewPath, [System.EnvironmentVariableTarget]::User)

# Set Path in the current session
$Env:PATH = "$dir\bin;$Env:PATH"

How to check if the path exists:

1
2
3
4
5
$Path = "C:\Program Files\Java\jdk-17.0.10\bin"
$PersistedPaths = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::User) -split ';'
if ($PersistedPaths -contains $Path) {
    Write-Host "Exist!"
}

How to set env variable in the current session:

1
[System.Environment]::SetEnvironmentVariable('host', "127.0.0.1")

4. Download File

1
2
3
$Url = "https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jdk_x64_windows_hotspot_17.0.10_7.msi"
$OutFile = "C:\Users\Admin\Documents\OpenJDK17U-jdk_x64_windows_hotspot_17.0.10_7.msi"
Invoke-WebRequest -Uri $Url -OutFile $OutFile

5. Get the current path of the running script

1
2
3
4
5
6
7
8
9
10
$ScriptPath = $($MyInvocation.MyCommand.Path)
Write-Host $ScriptPath

# Get the parent path of that script
$ParentPath = $([System.IO.Path]::GetFullPath("$($MyInvocation.MyCommand.Path)\.."))
Write-Host $ParentPath

# Get the current directory path
$CurrentPath = (Get-Item .).FullName
Write-Host $CurrentPath

6. Start a process in background

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Start a Java application in background
Start-Process javaw -ArgumentList "-Dlog4j.configurationFile=log4j2.properties",
        "-jar",
        "app.jar",
        "--spring.config.location=\config\" -WindowStyle Hidden

# Start a Python application in background
Start-Process -FilePath "python" -ArgumentList "$PythonScript" -RedirectStandardError "$PathLogErr" -WindowStyle Hidden

# Run exe file
Start-Process -FilePath "msiexec.exe" -ArgumentList "/i `"$InstallerFilePath`" /qn INSTALLDIR=`"$InstallDir`" /passive" -Wait -Verb RunAs

# Run msi installer file
Start-Process msiexec -Wait -ArgumentList "/i `"$InstallerFilePath`" /passive ADDLOCAL=`"all`""

7. Expand Archive File

1
Expand-Archive -Path $ZipFilePath -DestinationPath $DirPath -Force:True

8. Set timezone

1
Set-TimeZone -Id "Tokyo Standard Time"

9. Remove item

1
2
3
4
5
# Remove all files from the folder
Remove-Item -LiteralPath "parentFolder/childFolder" -Force -Recurse

# Clean all files by extensions
Get-ChildItem -Path $ServicePath -Include *.jar,*.war,*.pyc -Recurse | Remove-Item
This post is licensed under CC BY 4.0 by the author.