Home > Software engineering >  Use PowerShell to change the Env:Path in a mounted windows image
Use PowerShell to change the Env:Path in a mounted windows image

Time:11-23

I've written a PowerShell script to create a custom Boot.wim file that contains some useful utilities and custom .ps1 scripts along with the PowerShell executable. This all works just as it should. What I can't figure is how to add to the environment path variable the directory for the utilities "x:\Users\Utilities" to the Mounted Image.

Here's a code snippet from the program that shows how I manipulate the mounted image to add the utilities, etc.

$WinADK="C:\Program Files (x86)\Windows Kits\10\"  
        "Assessment and Deployment Kit"  
        "\Windows Preinstallation Environment\"  
        "$(Get-ArchitectureString)"
    
$WinPETemp='G:\TempPE'

New-Item -ItemType Directory -Path "$($WinPETemp)\Media" -force
$CIArgs = @{path        = $(Join-Path $WinAdk -ChildPath "en-us\winpe.wim")
            Destination = "$($WinPETemp)\Media\boot.wim"}
Copy-Item @CIArgs
New-Item -ItemType Directory -Path $(Join-Path $WinPETemp -ChildPath "\Mount") –Force

#----- Mount the WinPE WIM file for Editing -----

$MTArgs = @{ImagePath = $(Join-Path $($WinPETemp) -ChildPath "\Media\boot.wim")
            Index     = 1 
            path      = $(Join-Path $($WinPETemp) -ChildPath "\Mount")
           }

Try { Mount-WindowsImage @MTArgs -ErrorAction Stop}
Catch { 
        Get-WindowsImage -mounted | 
          Dismount-Windowsimage -discard
        Mount-WindowsImage @MTArgs
}
#--- Packages List - PowerShell, Storage, and DISM cmdlets --#
$PkgList = @(
    "WinPE-WMI.cab", 
    "en-us\WinPE-WMI_en-us.cab", 
    "WinPE-NetFx.cab", 
    "en-us\WinPE-NetFx_en-us.cab", 
    "WinPE-Scripting.cab", 
    "en-us\WinPE-Scripting_en-us.cab", 
    "WinPE-PowerShell.cab", 
    "en-us\WinPE-PowerShell_en-us.cab", 
    "WinPE-DismCmdlets.cab", 
    "en-us\WinPE-DismCmdlets_en-us.cab", 
    "WinPE-EnhancedStorage.cab", 
    "en-us\WinPE-EnhancedStorage_en-us.cab", 
    "WinPE-StorageWMI.cab", 
    "en-us\WinPE-StorageWMI_en-us.cab") 

 ForEach ($Pkg in $PkgList) {
 $AWPArgs = @{PackagePath = "$($WinAdk)\Winpe_OCS\$($Pkg)"
              Path        = "$($WinPETemp)\Mount"
              IgnoreCheck = $true }

 Add-WindowsPackage @AWPArgs 

 } #End ForEach ($Pkg...

<#
  Add your modifications now...
  Such as Directory for User Scripts, 
  Registry Modifications, specifically Powershell 
  Execution Policy
#>

#--- Add User Directories ---
$UserDirectories = @("Utilities","Scripts")

ForEach ($Dir in $UserDirectories) {
  $NIArgs = @{ItemType = 'Directory' 
              Path     = "$($WinPETemp)\Mount\Users\$($Dir)"}
  New-Item @NIArgs
}

#--- Add PowerShell Profile ---
$CIArgs = @{Path        = 
 "G:\WinPE Include Files\Microsoft.PowerShell_profile.ps1"
            Destination = 
 "$($WinPETemp)\Mount\Windows\System32\WindowsPowerShell\V1.0"}

 Copy-Item @CIArgs

#--- Add User Scripts to Directory ---

$Scripts = @("Get-DotNetVersionsInstalled.ps1")

ForEach ($FN in $Scripts) {
  $CIArgs = @{Path        = "G:\BEKDocs\Scripts\Production\$FN" 
              Destination = "$($WinPETemp)\Mount\Users\Scripts"}
  Copy-Item @CIArgs
}

#--- Add Portable Pgms to the Utilities Directory     ---
#--- Note: Must be 64 bit Programs no support for WOW ---

$Utilities = @("Speccy64\Speccy64.exe",
               "HWiNFO64\HWiNFO64.exe",
               "NirSoftx64\*.*")

ForEach ($FN in $Utilities) {
  $CIArgs = @{Path        = "G:\BEKDocs\NonInstPrograms\$FN" 
              Destination = "$($WinPETemp)\Mount\Users\Utilities"
              Recurse     = $True
             }

  If (Test-Path -Path "$(($CIArgs).Path)" ) {
    Copy-Item @CIArgs
  }
  Else {
         "Error:"   $CIArgs.Path   " Does NOT Exist!"
  }

}

#--- Mount WinPE Registry to the Local Registry for mods ---
$RegLoc = "$($WinPETemp)\Mount\Windows\System32\config\software"
reg load "HKLM\WimPE" "$RegLoc"

#--- Modify PowerShell Execution Policy in WIM Registry ---
$RegPath = "HKLM:\WimPE\Microsoft\PowerShell\1\ShellIds"  
           "\Microsoft.PowerShell\"
$NIArgs = @{Path  = $RegPath 
            Name  = 'ExecutionPolicy' 
            Value = "RemoteSigned"
            PropertyType = 'String'
            Force = $True}

New-ItemProperty @NIArgs

reg unload "HKLM\WimPE"  #--- UnMount WinPE registry ---

#----- DisMount the Modified WinPE WIM File -----

Dismount-WindowsImage -path "$($WinPETemp)\Mount" -Save

#----- Make a Backup of the Modified WIM file -----

$Destination='G:\Pewim'
New-Item -Path $Destination -ItemType Directory –Force

$CIArgs = @{Path        = "$($WinPETemp)\Media\boot.wim" 
            Destination = "$($Destination)\"}
Copy-Item @CIArgs

CodePudding user response:

You can put that into the image's registry, at HKLM:\SYSTEM\<ControlSet>\Control\Session Manager\Environment.

"ControlSet" is usually 001 and 002. Don't use CurrentControlSet though as that is not available for offline images.

CodePudding user response:

This answer is just to show the code I wrote, for other seekers information, after Ralph's fine suggestion. All is working as desired!

#--- Reset to load HKLM\System to add item to Path ---
$RegLoc = "G:\TempPE\Mount\Windows\System32\config\System"
reg load "HKLM\WimPE" "$RegLoc"
 
$RegPath = "HKLM:\WimPE\ControlSet001\Control\"  
           "Session Manager\Environment"

$GIArgs = @{Path  = "$RegPath" 
            Name  = 'Path' 
           }
$CurPath = (Get-ItemProperty @GIArgs).Path

$Curpath = "$CurPath;X:\Users\Utilities"

$SIArgs = @{Path  = "$RegPath"
            Name  = "Path"
            Value = "$CurPath"}
Set-ItemProperty @SIArgs

reg unload "HKLM\WimPE"  #--- UnMount WinPE registry ---

The above code goes right after the [ reg unload "HKLM\WimPe" ] in the code displayed in the question.

  • Related