During the installation process of our software we use a batch file that replaces some text in some configuration files depending on user's input. The batch file basically calls a PowerShell command to do so:
REM %1 - file name e.g. file.txt
REM %2 - variable placeholder text e.g. {%placeholder%}
REM %3 - text to replace variable placeholder e.g. BLAH
powershell -Command "(gc %1) -replace '%2', '%3' | Out-File -encoding UTF8 %1" || echo ErrorLevel %errorlevel% > injection-bat.err
Some customers report that the batch file is failing. After some research on the failing machines we're able to see that PowerShell does not recognize the "Get-Content" (gc) cmdlet:
The term 'Get-Content' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
By taking a look on the installed PowerShell modules in the failing machine I was able to see that the Microsoft.PowerShell.Management module is not present. However, on all machines we've tested, the module is there (even in a clean fresh Windows installation).
Since I could not find a clear answer on that, I ask it here ...
Why so a basic module (Microsoft.PowerShell.Management) is not present in a Windows machine? Is there a list of "preinstalled" modules in Windows we can assure are always there? Would it be better to somehow install/import those modules using the batch file before using them just to be on the safe side? If this module is preinstalled with Windows, is there a reason why the module could be removed from the machine?
Many thanks in advance.
UPDATE 1:
I was able to find the PS version installed -> 5.1
The module Microsoft.PowerShell.Management is not in the list of available modules (Get-Module -ListAvailable).
UPDATE 2:
Here I answer the questions from @leeharvey1:
Does $Env:PSModulePath list C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules?
Yes
C:\Users\MAN Langenhagen\Documents\WindowsPowerShell\Modules;
C:\Program Files\WindowsPowerShell\Modules;
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
Does C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Management\Microsoft.PowerShell.Management.psd1 exist?
Yes
Is Microsoft.PowerShell.Commands.Management.dll present in your %PATH% (eg, $env:Path -split ';' | % { dir "$_\Microsoft.PowerShell.Commands.Management.dll" -ea silentlycontinue })?
I was not able to execute the command. But a Windows search showed 4 matches of the dll in:
C:\Windows\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\1.0.0.0__31bf3856ad364e35
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\v4.0_3.0.0.0__31bf3856ad364e35
C:\Windows\WinSxS\msil_microsoft.powershell.commands.management_31bf3856ad364e35_1.0.0.0_none_9fba00a194c2da2a
C:\Windows\WinSxS\msil_microsoft.powershell.commands.management_31bf3856ad364e35_10.0.19041.1_none_fcfa075fee21fc1f
However, these locations seem not to be part of the PATH (unless recursive).
C:\Program Files (x86)\Intel\iCLS Client\
C:\Program Files\Intel\iCLS Client\
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0\
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL
C:\Program Files\Intel\Intel(R) Management Engine Components\DAL
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT
C:\Program Files\Intel\Intel(R) Management Engine Components\IPT
C:\Program Files\Intel\WiFi\bin\
C:\Program Files\Common Files\Intel\WirelessCommon\
C:\WINDOWS\System32\OpenSSH\
C:\Users\User\AppData\Local\Microsoft\WindowsApps
Is a .NET Framework 4 version installed (eg, [System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription)?
That command returned nothing, however the call [System.Runtime.InteropServices.RuntimeInformation] returned:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False RuntimeInformation System.Object