PowerShell script runs in ISE but not as a script - powershell

OBJECTIVES :-
I'm having trouble with something that should be relatively simple? Searched similar questions here but no cigar. I dont want to have to pass a command to the file as a Ps1 script (common suggestion). I'm hoping I can just right click, run with PowerShell and it works.
SITUATION :-
I have to automate uninstalling of software often. Rather than scour the registry for msiexec strings, I edited some PS code so I can search for what I want, then do whatever I need to do with the info I receive. The below runs fine in PowerShell ISE, but as a PS1 script (right click, run with PS) it outputs nothing on the screen.
CODE :-
$Prog = Read-Host -Prompt 'Input your Prog name'
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match "$Prog"} | Select-Object -Property DisplayName, UninstallString
pause
Lets say at the prompt I search for 7-Zip in ISE I get this...
Input your Prog name: 7-zip
DisplayName UninstallString
----------- ---------------
7-Zip 19.00 (x64) C:\Program Files\7-Zip\Uninstall.exe
Running as a script, I get nothing output into the console?
Input your Prog name: 7-zip
Press Enter to continue...:
QUESTION :- What do I need to do to make the script behave like it does in the ISE console?
Many thanks for any assistance
P.S (<see what I did there, I'm new to the PowerShell game, thanks>)

It works in Powershell too, if you run it from Powershell Interactive Prompt or run using powershell -file "FILEPATH" in CMD/PowerShell, the output remains on the screen. But when you run directly with a double-click output doesn't stay persistent on the screen, instead when it reaches EOF then it quits. So, Add Read-Host or cmd /c pause at the end to stop the program at the end, to view output.

Related

Is it possible to use PowerShell to automate interactions with installed programs in windows 10?

Is it possible, from a PowerShell script, to for example automate text box population, automate striking of the enter key inside of an installed program in Windows 10? Could anyone point me in the direction of relevant documentation?
Would a different alternative to PowerShell be more appropriate?
Thanks!
I know how to execute the program from PowerShell (PS C:> 'filepath\program.exe'). I also know how to kill the program from PowerShell (PS C:> stop-process -Name "program_name").
In trying to figure out how events are identified, I tried:
PS C:> $Events = Get-Event
PS C:> $Events[0] | Format-List -Property *
But it returns an error message: "Cannot index into a null array"

powershell cmd executed in ISE but not as ps1

I am facing a peculiar issue.
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion |
Format-Table –AutoSize > C:\temp\InstalledSoftwareList.txt
When I run the above command from ISE, it creates the file and provides the expected output to txt file.
But when I save as PS1 and run with double click or by opening powershell console and run, it just creates txt file with no output.
I tried changing the working directory, admin user etc but no use. Suggestions please
It is PowerShell 5 and Windows 2016 R2

Get-PsDrive Description not showing drive name

I am trying to get the location of a usb named Foobar.
Running (Get-PsDrive -Name D).Description works in Powershell ISE but not in Powershell.
When running in Powershell ISE the result is Foobar
When running in Powershell I get nothing back (empty string)
What is the difference when running the same command in ISE and in the normal powershell?
Also is it possible to get a drive location reliably by the name/label of the device?
Thomas, I tested using this script:
clear-host
$x = Get-Psdrive | Where-object Name -like "?"
ForEach ($drv in $x) {
"$($drv.name) : $($drv.Description)"
}
I got the following result on 5.1 ISE 5.1.18362.1171 & CMD 5.1.18362.1171 and also on PS7 7.0.0 (all 64 Bit):
Since you're checking for D, I'm assuming it is a CD drive. Thus, it appears to be normal behavior? Not sure why you got different results.

Combining powershell & cmd to create a batch file

I am not a coder but I have some basic ability to mash some scripts together and usually get something that works for me, but this is way beyond my comprehension. I am trying to combine 3 things into a batch file. It's essentially an all in one solution to turn Windows 10 privacy settings off and to remove some background apps. I can run the commands just fine in powershell, but I wanted to automate the entire thing through one batch script:
Keep the windows 10 privacy settings script up to date. This is done through the following command in powershell:
(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/hahndorf/Set-Privacy/master/Set-Privacy.ps1') | out-file .\Set-Privacy.ps1 -force
Just need to make sure that the destination folder for download is set first (I have no idea how to do this through a batchfile/script). The command comes from the script that someone wrote on github located here
Run the powershell script to turn privacy settings off, on the highest setting. This is done through the following command, again from the link above:
.\Set-Privacy.ps1 -Strong -admin
Remove background apps, for example, removing "3D Builder":
To uninstall 3D Builder:
get-appxpackage *3dbuilder* | remove-appxpackage
Details on this found here
Any help with any of this is greatly appreciated.
Try this, of course PowerShell is not a batch file tool per se, though you can use it to call a batch (.bat,.cmd.vbs, etc.) file.
Though a script is a script (batch or otherwise), taxonomy is a whole different conversation.
So, what you want is a function you can call as needed.
Function Set-Windows10PrivacySettings
{
[CmdletBinding()]
[Alias()]
Param
(
[string]$DownloadPath = "$env:USERPROFILE\Downloads"
)
# Download the Privacy Script
(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/hahndorf/Set-Privacy/master/Set-Privacy.ps1') |
out-file "$env:USERPROFILE\Downloads\Set-Privacy.ps1"
# Temporarialy naviagte to the target directory adn run the script
Push-Location -Path $DownloadPath
Start-Process .\Set-Privacy.ps1 -Strong -admin
# Remove AppX packages
$AppPackageList = (Get-AppxPackage).Name |
Sort-Object |
Out-GridView -Title 'Select one or more AppX to remove. Press CRTL+LeftMouseClick to multi-select' -PassThru
# Remove the selected AppX without prompt to confirm
ForEach($AppPackage in $AppPackageList)
{
"Removing selected AppX $AppPackage"
# Remove-AppxPackage -Package $_ -Confirm:$false -WhatIf
}
# Retrun to the original directory
Pop-Location
}
# Call the function in PowerShell
Set-Windows10PrivacySettings
Just remove the comment marker '#' and the 'WhatIf' switch to actually allow things to happen.

Running Powershell script within Powershell 'silently'

My setup:
One Powershell script with a menu. Upon selecting a a menu item, a specific Powershell script is run assigned to that menu item.
I'm attempting to make this second Powershell script run silently as in - nothing is shown in the main Powershell script.
Bonus:
The full setup I'm looking at is:
User runs Powershell script
User selects a menu item (this executes another Powershell script)
The information in the other Powershell script is not shown to the user
After the second Powershell script has been executed, the user is shown a line of text such as: "Script has been run successfully"
What I've tried:
Adding the following code to each secondary script
$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
Unfortunately this closes the main Powershell window which isn't what I desire.
You could run the secondary command as a background-job and wait for it to finish. That way no output is shown in the shell
Start-Job -FilePath YourSecondaryScript.ps1 | Wait-Job
if you need, you can still analyze the job result (the script output)
Fixed by appending
| out-null
at the end of each line of code in the secondary scripts.