0

Can anyone help me to create a Windows batch script equivalent of Windows PowerShell script that I wrote. I know what first few lines should be, but I am not sure for the rest.

PowerShell Code:

Get-Process -Name Firefox | Stop-Process

Start-Process -FilePath "C:\Program Files\Mozilla Firefox\uninstall\helper.exe" -ArgumentList "-ms" -ErrorAction SilentlyContinue -PassThru | Wait-Process -Timeout 30
Start-Process -FilePath "C:\Program Files (x86)\Mozilla Firefox\uninstall\helper.exe" -ArgumentList "-ms" -ErrorAction SilentlyContinue -PassThru | Wait-Process -Timeout 30

$UserProfiles = Get-ChildItem -Path "C:\Users" -Exclude "Public", "Default"

foreach ($Profile in $UserProfiles) {
    Remove-Item -Path "$Profile\AppData\Roaming\Mozilla\Firefox" -Force -Recurse -ErrorAction SilentlyContinue -Confirm:$false
}

Batch Code:

taskill Firefox
"C:\Program Files\Mozilla Firefox\uninstall\helper.exe" -ms
"C:\Program Files (x86)\Mozilla Firefox\uninstall\helper.exe" -ms
...
5
  • 1
    What's wrong with using the PowerShell code?
    – DavidPostill
    Commented May 15, 2021 at 20:35
  • @DavidPostill I would preferred to use PowerShell but the script won't run on the remote computers because of PowerShell version on those remote computers. Commented May 15, 2021 at 20:48
  • 3
    As for this I would preferred to use PowerShell but the script won't run on the remote computers because of PowerShell version on those remote computers., then write the script to the lowest version of PowerShell in your environment, or write your script to have code segments for each version in your environment, check the version and run that branch. There is zero reason to flip to batch for this, if PowerShell (any version) already exists on hosts. I am not seeing anything in the posted script as being version-specific, other than Wait-Process. Start-Process has a -wait switch.
    – postanote
    Commented May 15, 2021 at 23:06
  • There is not a native way to do that wait-process with a timer thing in batch either; not without writing more code. Since you are already trying to do that in a batch file, just refactor your script for the lowest version in your environment. You should be writing code that is to run in your environment, not what's on your personal host. Powershell v2 cmdlets
    – postanote
    Commented May 15, 2021 at 23:10
  • 1
    Following on from @postanote if you need to test if your script is compatible with version 2 on your local machine you can execute the script using the following powershell -version 2 <Path-to-Script>, this may need some windows features enabling to accomplish (been a while since I have done this myself). Please note that the version switch is only able to accept the value of 2 otherwise it will try to run with the latest version.
    – CraftyB
    Commented May 16, 2021 at 10:09

1 Answer 1

1
@echo off && Setlocal EnableDelayedExpansion

set^ "_Mozilla_Firefox=AppData\Roaming\Mozilla\Firefox\." 
set^ "_Mozilla_helper_=Mozilla Firefox\uninstall\helper.exe"
set^ "_ProgramFiles=ProgramFiles,ProgramFiles(x86^),ProgramW6432"
set^ "_wmic=^<con: %__AppDir__%\wbem\wmic.exe" & cd /d "%__AppDir__%" 

:again
tasklist.exe /svc /fo list|find.exe /i "firefox.exe" >nul && 2>nul (( 
    taskkill.exe /f /im firefox.exe >nul ) || ( timeout.exe /t 001 >nul & goto :again ))
     
for %%i in (!_ProgramFiles!)do if exist "!%%~i!\!_Mozilla_helper_!" (
    start "Unistall Mozilla Firefox.exe" /Wait /Normal "!%%~i!\!_Mozilla_helper_!" -ms )

<nul cd /d "%UserProfile%" && cd.. && for /f usebackq^ ^tokens^=1*^ ^delims^=^= %%i in (
`%_wmic% UserAccount where status^='ok' get name/format:list^|%__AppDir__%more.com^|findstr.exe .
   `)do call set^ "_%%~i=%%~j" && <con: call rmDir /q /s "!__cd__!\!_name!\!_Mozilla_Firefox!" 2>nul 
         
popd & endlocal & goto :eof

Obs.: Try to think of a bat to perform the same task, it is easier than searching for a "translation" involving methods on ps1 that will not exist in cmd.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .