Removing regedit key via powershell - powershell

I have been removing powershell and/or command line like below. My question is : Is there any equivalent inside powershell for /reg:64 parameter ?
CMD version:
reg delete HKEY_LOCAL_MACHINE\SOFTWARE\TEST /v PropertyToRemove /f /reg:64
powershell version:
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client" -Name "PropertyToRemove"
thanks,

It looks like it's not implemented.
Reading this question: Querying via powershell both 32bit and 64bit registry
it seems you should just be checking whether the os is x32 or x64 first
or run a check if the 64bit registry keys exists
Different topic, same question.. here's stated it's not implemented:
How to access the 64-bit registry from a 32-bit Powershell instance?
look at whenrybruce's reply on the marked answer

Related

Command Prompt executes this correctly but Powershell doesn't?

Having some weirdness where Powershell is not executing this command correctly. It can't understand the INSTALLFOLDER or TARGETDIR and just throws up the Windows Installer help box.
Whereas command prompt processes this just fine??? It's very weird. PS version is 5.1.18362 I believe.
the command is
msiexec.exe /i "C:\Users\sadas\aasasdd\sda\asdasd\19526-Debug-x64.msi" INSTALLFOLDER="C:\Installation Test" /qn
My msi is a Wix installer msi and has the property INSTALLFOLDER
This is not how you run this command in PowerShell. There are several ways to run external command in PowerShell and it a well-documented use case. Links to follow
• Using PowerShell and external commands and their parameters or
switches.
Running external commands, always require special consideration.
• PowerShell: Running Executables
• Solve Problems with External Command Lines in PowerShell
• Top 5 tips for running external commands in Powershell
• Using Windows PowerShell to run old command-line tools (and their
weirdest parameters)
• Execution of external commands in PowerShell done right
https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right
https://mnaoumov.wordpress.com/2015/03/31/execution-of-external-commands-native-applications-in-powershell-done-right-part-2
https://mnaoumov.wordpress.com/2015/04/05/execution-of-external-commands-native-applications-in-powershell-done-right-part-3
http://edgylogic.com/blog/powershell-and-external-commands-done-right
• Quoating specifics
https://trevorsullivan.net/2016/07/20/powershell-quoting
PowerShell has a list of characters that mean specific things, vs what cmd.exe implementation means, and if you need those, they must be properly terminated. See the below
About Special Characters
PowerShell - Special Characters And Tokens
So, to make your command work in the PowerShell consolehost, ISE, VSCode, do something like this...
# Using teh Call operator
& 'msiexec.exe /i "C:\Users\sadas\aasasdd\sda\asdasd\19526-Debug-x64.msi" INSTALLFOLDER="C:\Installation Test" /qn'
or
... this...
# Using Start-Process
$ConsoelCommand = 'msiexec.exe /i "C:\Users\sadas\aasasdd\sda\asdasd\19526-Debug-x64.msi" INSTALLFOLDER="C:\Installation Test" /qn'
Start-Process powershell -ArgumentList "-NoExit","-Command &{ $ConsoleCommand }" -Wait
Make sure you check for existence of that actual folder, but it's likely the problem is the space
instead of this:
msiexec.exe /i "C:\Users\sadas\aasasdd\sda\asdasd\19526-Debug-x64.msi" INSTALLFOLDER="C:\Installation Test" /qn
try this:
invoke-command -scriptblock {cmd /c "msiexec.exe /i `"C:\Users\sadas\aasasdd\sda\asdasd\19526-Debug-x64.msi`" INSTALLFOLDER=`"C:\Installation` Test`" /qn"}
Notice that all the double-quotes are escaped AND a the space in "C:\Installation Test" is escaped. Your problem will boil down to this space and how you handle it. Also try changing double to single quotes. If you decide to use variables in your command, definitely add the strings (""+"") to create the command so that it will expand the variables correctly.

Powershell not finding PNPUTIL when script launched from shortcut

I have a Powershell script to install TCP/IP printers on Windows 10 that uses PNPUTIL to load drivers. When the script is run from a Powershell window, everything works great.
When I launch the script from a shortcut using the format
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -file MyScript.PS1
I get an error 'The term 'pnputil.exe' is not recognized as the name of a cmdlet, function, script file, or operable program' when PNPUTIL is called. The rest of the script runs fine.
Relevant code:
Write-Host `n 'Installing printer driver..'
pnputil.exe /add-driver "\\myServer\HP UPD PCL 5\hpcu180t.inf"
Any ideas as to why this won't work when launched from a shortcut?
EDIT:I tried using
& pnputil.exe /add-driver "\\myServer\HP UPD PCL 5\hpcu180t.inf"
as referenced in
Running CMD command in PowerShell
but I still get the error. I also tried
start-process pnputil.exe /add-driver "\\myServer\HP UPD PCL 5\hpcu180t.inf"
but got a similar error that pnputil.exe could not be found.
Both of these options work from a Powershell prompt, but again, fail when launched from a shortcut.
Thank you in advance.
You're invoking a 32-bit instance of PowerShell on a 64-bit system, and that instance doesn't see pnputil.exe (by filename only).
Instead of:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -file MyScript.PS1
use:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file MyScript.PS1
Folder C:\Windows\SysWOW64 is where the 32-bit executables live.
Paradoxically, for historical reasons, it is C:\Windows\System32 that houses the 64-bit executables.
If, for some reason, you do need to run a 32-bit instance of PowerShell, you can invoke pnputil.exe by its full path:
It only exists as a 64-bit executable in the 64-bit system folder, which 32-bit processes can access as C:\Windows\SysNative:
C:\Windows\SysNative\pnputil.exe

The specified path is not valid

I am in a windows console or powershell. (Windows7 x64 Pro, PowerShell 4)
When i try to type command like "cmd" i have an error message "The specified path is not valid"
PS D:\DevEnv\workspace\api-node> cmd
Microsoft Windows [version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All right reserved.
The specified path is not valid.
What i tryed after reading msdn articles but it don't solve the problem:
-delete my System variable PATH and try again
-clean my registry with ccleaner
Is there any "verbose" or "debug" mode in powershell or windows console to see what path is involved and where i can change it?
Is there any "verbose" or "debug" mode …? I don't think so; however, running next commands from an open cmd window could help to identify possible error source(s):
wmic process where "name='cmd.exe'" get Caption, CommandLine, ParentProcessId, ProcessId
2>NUL reg query "HKLM\Software\Microsoft\Command Processor" /V AutoRun
2>NUL reg query "HKCU\Software\Microsoft\Command Processor" /V AutoRun
In above output we are seeking for any commands which could be a source of The specified path is not valid error message.
wmic seems to be self-explaining;
both reg query show AutoRun registry values (if present), see cmd /?.

Windows service environment - what am I doing wrong?

I'm installing my own Windows service. It requires it's own Environment. No tool that I know handles it, so I manually edit registry.
I have service installation script written in PowerShell, so my first idea was to do this:
New-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\services\MyService" -Name Environment -Value "EnvVar=value" -PropertyType MultiString
The problem is, while the key appears int the registry, it is not used by my service, even after powercycle.
Experimenting, I found out that if I edit it in regedit, it works and the key is used by my service. So, I hanged it to this:
Invoke-Expression "reg.exe add ""HKLM\SYSTEM\CurrentControlSet\services\$name"" /v ""Environment"" /d ""EnvVar=value"" /t REG_MULTI_SZ /f"
and now it works :D
Whats wrong with my usage of New-ItemProperty? I'm doing everything as admin, and I'm avare of 64/32bit issues (and I don't think they apply here - my software is 64bit, windows is 64bit, everything is 64bit :))
It adds the key to the registry but it is not used, while adding the same key using regedit.exe or reg.exe works fine. Maybe I'm hitting some bug? (OS: Windows Server 2012, 64bit)

Automate Software Updates from batch

Can someone please help me figure out the code for Windows batch file that will look a the properties of executables or installers in a directory, determine the version of the installer and the name of the product, store them in variables, and query agaist installed products in the registry? The idea is that I would like to copy updates to a folder on a machine that has no internet connection from disc. From there I would like to execute a batch file that looks at the version and name of software, stores them in variables, then queries the registry to see if there is a previous version installed. So, if I had downloaded install_flash_player_ax.exe, it should know to look in the registry for adobe flashplayer. If the version on the executable is newer than the version in the registry, it would do a quiet install.
Any help or suggestions would be greatly appreciated!
Though it is an old question, I'd try to answer it, as it may be useful for the others.
Windows command shell doesn't have a straight way to get file metadata like version but you can use wmic for it.
The main problem is that the display name of the software may be different in the properties of the installation/update exe file and than the one in the registry. So it is a bad idea to take the name from the file metadata and query for it in whole HKLM registry hive.
Also, if you do not have a predefined list of the software to be updated and do not know the exact path in the registry where version for each of them is stored, the idea to loop against the list of exe to get names from their metadata id also bad.
So, the best way to search for that is to make a script for each exe separately and to add them to Windows scheduler.
Here is an example of the required batch script to automate updates for Adobe Flash Player for 64-bit OS:
#echo off
for /f %%a in ('wmic datafile where name^="C:\\Users\\username\\Downloads\\install_flash_player_19_active_x.exe" get version ^| find /n /v "" ^| findstr "^\[2\]"') do set var=%%a
for /f "tokens=2 delims=]" %%a in ("%var%") do set prver=%%a
echo Available version: %prver%
for /f "tokens=3" %%a in ('reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{EE56217C-B3F9-402B-B4EC-63F090F51D3D}" /v DisplayVersion') do set regversion=%%a
echo Installed version: %regversion%
if %prver% == %regversion% (echo The newest version %regversion% installed) else (echo Update required & "C:\Users\username\Downloads\install_flash_player_19_active_x.exe")
Update file is located in some local folder, in my case C:\Users\username\Downloads\install_flash_player_19_active_x.exe. When programs are installed, they register themselves in
HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ for 64-bot OS and
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ for 32-bit.
Hence, you need to find the path for every installation you need. Please note that {EE56217C-B3F9-402B-B4EC-63F090F51D3D} in my script is the GUID for the given version of Flash Player 19.
Here is the same in PowerShell which is more elegant:
$filever = (Get-Item "C:\Users\username\Downloads\install_flash_player_19_active_x.exe").versioninfo.fileversion
$appname = (Get-Item "C:\Users\username\Downloads\install_flash_player_19_active_x.exe").versioninfo.internalname
$regpath = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{EE56217C-B3F9-402B-B4EC-63F090F51D3D}"
$regversion = Get-ItemProperty $regpath -Name "DisplayVersion" | select -ExpandProperty "DisplayVersion"
if ($winrarreg -eq $regversion) {
"The newest version of Flash Player $regpath is already installed"
} else {
"Current installed version is:" + $regversion
"Available version is:" + $filever
"Let's update Flash Player"
Start-Process -FilePath "C:\Users\username\Downloads\install_flash_player_19_active_x.exe"
}