0

I've been trying to find an answer to this peculiar question but haven't had any luck. For work, I've been creating a tiny little script that would help my colleagues and I with the repetitive tasks of preconfiguring new laptops for our clients.

I'm aware that what I'm going to ask is probably not the most efficient method, but I am curious regardless!

I'm at the point in my little script where I want it to write the lines for a temporary second script, run it and then remove it afterwards. However, I fail to have this work correctly. I'm assuming this is because of special characters but I could be wrong.

The method I'm trying to use is to have it echo the actual commands to a file. In the end, I'm hoping to have it create a working .PS1 script, execute it and then delete it.

The commands I want it to write to a file:

$OfficeUninstallStrings = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where {$_.DisplayName -like "*Microsoft Office 365*"} | Select UninstallString).UninstallString
ForEach ($UninstallString in $OfficeUninstallStrings) {
    $UninstallEXE = ($UninstallString -split '"')[1]
    $UninstallArg = ($UninstallString -split '"')[2] + " DisplayLevel=False"
    Start-Process -FilePath $UninstallEXE -ArgumentList $UninstallArg -Wait
}    

Would anyone be able to help me figure out what I'm overlooking?

Thank you!

1
  • As an alternative, you might want to generate the script based on a template that contains powershell variables that get resolved at template expansion time, driven by the data. I wrote a little tool that does this for CSV data. You can find my litle tool here: stackoverflow.com/questions/42230306/… Nov 17, 2022 at 12:44

1 Answer 1

1

This whole concept seems a little wonkie. Why not just use the script and run that? .. ok .. whatever .. you do you. :)

The script below creates your file in the temp directory in a folder using a random folder name under that.

If you want it cleaned up, you need to remove the echo command in front of the rd /s /q command. I am not going to do that for you.

Have you thought about powershell's execution-policy?

If you need to set the execution policy, run the batch as an admin and change the PowerShell command (in the batch) to:
PowerShell.exe -ExecutionPolicy Bypass -File "%wholePS1Path%"

If that doesn't work, here are some other methods.

  @echo off
  setlocal

  set outputFile=uninstall_office.PS1
  set outputFilePath=%TEMP%\%RANDOM%
  set wholePS1Path="%outputFilePath%\%outputFile%"

  echo $OfficeUninstallStrings = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* ^| Where {$_.DisplayName -like "*Microsoft Office 365*"} ^| Select UninstallString).UninstallString >%outputFile%
  echo ForEach ($UninstallString in $OfficeUninstallStrings) { >>%outputFile%
  echo     $UninstallEXE = ($UninstallString -split ^'^"^')[1] >>%outputFile%
  echo     $UninstallArg = ($UninstallString -split ^'^"^')[2] + " DisplayLevel=False" >>%outputFile%
  echo     Start-Process -FilePath $UninstallEXE -ArgumentList $UninstallArg -Wait >>%outputFile%
  echo } >>%outputFile%

  :: Run the powershell command
  powershell -File "%wholePS1Path%"
  set powershell_error=%ERRORLEVEL%

  :: Remove this line to clean up once you are SURE it is working.
  :: Of course, rd can be brutal.
  echo rd /s /q "%outputFilePath%"

  endlocal && exit /b %powershell_error%

2
  • Thank you. Yes, the whole idea to do this is a little silly, but this answers other questions for me as well. However, I noticed this would work when run normally, but does not seem to work when run as administrator, is there something I'm missing? I've edited it to create the output file on the desktop to check the creation of the file, which works, unless I run the batch as an administrator.
    – FoxNBeard
    Nov 16, 2022 at 16:23
  • Hrrmm.. unless you are literally using the account "Administrator" (as apposed to running elevated).. I am also stumped with that one. And glad to help. Nov 16, 2022 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.