Sleep or Pause to PowerShell Script

Last Updated on August 7, 2025 by Arnav Sharma

Here’s a quick fix if you see the modules installed in the system, but the commands don’t work.

Step 1: Run the get module command and and copy the locations (can be 2 or more)

Get-Module -ListAvailable

Scroll down to look for more locations.

Step 2: Run the following command to see if path(s) from step 1 are missing here

$Env:PSModulePath
[Environment]::GetEnvironmentVariable("PSModulePath")

Step 3: If the location(s) in Step 1 are missing in the output of Step 2, then add the locations using the below script:

#Save the current value in the $p variable.
$p = [Environment]::GetEnvironmentVariable("PSModulePath")

#Add the new path to the $p variable. Begin with a semi-colon separator.
$p += ";C:Program Files (x86)MyCompanyModules"  ## Update path here

#Add the paths in $p to the PSModulePath value.
[Environment]::SetEnvironmentVariable("PSModulePath",$p)

Step 4: Reboot the system and check if the path has been added.

To check if all paths are up-to-date and being used by PowerShell, run the below script and any missing paths will be reported in Red Colour (Green outputs are good)

# Step 1: Get all available modules and their directory paths
$modules = Get-Module -ListAvailable | Select-Object Name, ModuleBase

# Step 2: Get the list of directories in $env:PSModulePath
$modulePaths = $env:PSModulePath -split ";" # Splitting the paths

$missingPaths = @()

foreach ($module in $modules) {
    # Extract the parent directory path until "Modules"
    $moduleParentPath = Split-Path $module.ModuleBase -Parent

    # Ensure $moduleParentPath is not empty to avoid the error
    while ($moduleParentPath -and (Split-Path $moduleParentPath -Leaf) -ne 'Modules') {
        $moduleParentPath = Split-Path $moduleParentPath -Parent
    }

    # Check if the parent directory is present in $env:PSModulePath
    if ($moduleParentPath -and -not ($modulePaths -contains $moduleParentPath)) {
        Write-Host "$($module.Name) module directory ($($module.ModuleBase)) is missing from $env:PSModulePath." -ForegroundColor Red

        # Add the missing path to the list if not already present
        if (-not ($missingPaths -contains $moduleParentPath)) {
            $missingPaths += $moduleParentPath
        }
    }
}

# If there are missing paths, prompt the user to fix
if ($missingPaths.Count -gt 0) {
    $action = Read-Host "There are missing paths. Do you want to 'Fix' or 'Ignore'?"

    if ($action -eq 'Fix') {
        foreach ($path in $missingPaths) {
            $env:PSModulePath += ";$path"
        }
        Write-Host "Paths added to $env:PSModulePath for this session." -ForegroundColor Green
    } elseif ($action -eq 'Ignore') {
        Write-Host "Ignoring missing paths." -ForegroundColor Yellow
    } else {
        Write-Host "Invalid choice. Exiting without action." -ForegroundColor Yellow
    }
} else {
    Write-Host "All module paths are up to date." -ForegroundColor Green
}

And don’t forget to keep your modules updated: Remove Old PowerShell Modules (arnav.au)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.