Skip to main content

Running Built-in PowerShell Commands with Custom Fields

In FileWave, Windows PowerShell commands run from Custom Fields in a 32-bit context. Some built-in cmdlets require 64-bit PowerShell, so you need to launch the 64-bit executable explicitly.

The example below lists the members of the local Administrators group. If you run it directly as a PowerShell Custom Field, it may fail because FileWave starts it in 32-bit PowerShell.

Get-LocalGroupMember -Group 'Administrators' | Select Name

client script powershell.png

To run the command in 64-bit PowerShell, create the Custom Field as a BAT script and call the 64-bit executable first. Use the Sysnative path when the command is launched from a 32-bit process on a 64-bit Windows device.

C:\Windows\sysnative\windowsPowerShell\v1.0\powershell.exe "Get-LocalGroupMember -Group 'Administrators' | Select Name"
exit 0

client script powershell 2.png

If you need an entire script to relaunch in the native environment, use the following wrapper:

#############################################################################
# If PowerShell is running the 32-bit version on a 64-bit machine, we
# need to force PowerShell to run in 64-bit mode.
#############################################################################
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
    # write-warning "Take me to 64-bit....."
    if ($myInvocation.Line) {
        &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
    } else {
        &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
    }
    exit $lastexitcode
}

# Main script
# Uncomment the next line to prove that we are always in 64-bit
#[Environment]::Is64BitProcess

# Your 64-bit script here.

#############################################################################
# End
#############################################################################