powershell open url in a hidden window except IE - powershell

I would like to run a power shell command like Start-Process with browsers except IE to open in a hidden window. Is there any way to set size of browser to make it hidden when we run the powershell command.
Like; Start-Process -SomeCommandToOpenChromeHidden "chrome.exe" "www.google.com"
Any idea?
Thanks
RESOLVED:
I updated my IE to 11 then now it is fine with following command:
$ie = new-object -comobject InternetExplorer.Application $ie.visible = $false

There is -WindowStyle Parameter. Values can be: Normal, Hidden, Minimized, and Maximized. The default value is Normal.
So you need to use:
Start-Process -WindowStyle Hidden "chrome.exe" "www.google.com"

Related

How to run a powershell script in the background and receive a toast notification?

The answers to this question address the first part of my question.
Unfortunately when I schedule a job such that the powershell window is hidden, any toast notifications that my script sends are not shown.
The script shows toast notifications using this script (source):
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = 'What do you think of this balloon tip?'
$balloon.BalloonTipTitle = "Attention $Env:USERNAME"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
When I schedule the powershell script in a way that it's not hidden, notifications are shown, but every time the script is triggered I briefly see a powershell window.
How can I schedule a task or job that hides the powershell window, but shows toast notifications?
Powershell.exe is a console application. The console window is automatically created by the OS when the process starts. The powershell.exe code that processes -WindowStyle Hidden is therefore executed after the console window is opened hence the flash. I like to use VB script to start PowerShell to ensure a completely hidden experience
In, say ps-run_hidden.vbs put
Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
objShell.Run("powershell -windowstyle hidden -executionpolicy bypass -noninteractive ""&"" ""'" & arg & "'"""),0
Next
Then use it to run the command you want, e.g. from Windows' scheduled tasks like so
wscript "C:\Path\To\ps-run_hidden.vbs" "C:\Other\Path\To\your-script.ps1"
You can now run a task without seeing any flashing windows.
You can run it as a Job in the background that way no window pops up and you get your message displayed,
Note: Any variables declared outside of Start-Job will need to be used like $using:variable. If $pid is declared outside of the Start-Job, you will use $using:pid.
Start-Job -ScriptBlock {
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $using:pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = 'What do you think of this balloon tip?'
$balloon.BalloonTipTitle = "Attention $Env:USERNAME"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
} | Wait-Job | Remove-Job
Once you have the above code in your ps1 file, simply call the following line. It will not display powershell window and provide you with the notification window.
powershell -File myScript.ps1 -WindowStyle Hidden

Powershell script to send keys alt tab

I have an .exe file that I'm running using Start-Process. Once my application opens, I want to send first tab key and then do alt + tab.
I'm using following commands, but so far only my application is launched.
Start-Process -FilePath $Exe -WorkingDirectory $Path
Start-Sleep 10
[System.Windows.Forms.SendKeys]::SendWait(“{TAB}”)
Start-Sleep 2
[System.Windows.Forms.SendKeys]::SendWait(“%{TAB}”)
Use this:
Start-Process -FilePath $Exe -WorkingDirectory $Path
$wsh = New-Object -ComObject Wscript.Shell
$wsh.AppActivate("Window title of $exe")
Start-Sleep 10
$wsh.SendKeys("{TAB}")
Start-Sleep 2
$wsh.Sendkeys("%{TAB}")
You need to activate the exe file's window with its window title. Also prefer Wscript.Shell to send keys. Replace "Window title of $exe" with its window title.

Run CMD as admin in PowerShell

I hope this is simple.
$cred = Get-Credential $env:USERDOMAIN\$env:USERNAME
$credUser = $cred.UserName
$credPW = $cred.Password
$Col1Row2 = New-Object System.Windows.Forms.Button
$Col1Row2.Location = New-Object System.Drawing.Size(5,80)
$Col1Row2.Size = New-Object System.Drawing.Size(150,25)
$Col1Row2.Text = "CMD as Admin"
$Col1Row2.Add_Click({Write-Host "Started Command Prompt.";$x= RunAs /user:$credUser C:\Windows\System32\cmd.exe })
$objForm.Controls.Add($Col1Row2)
There's a button to start command prompt inside my form, but I want it to start as admin by catching the user credentials at the beginning and using them to run. The problem is I can't seem to figure out how to get this to work.
If I run it in the script, it doesn't seem to work at all (in ISE). If I run the line in PowerShell as RunAs /user:$credUser C:\Windows\System32\cmd.exe it asks for the password. If I run it with the password at the end it doesn't seem to care. Any assistance is appreciated.
$params=#("/C";"$x";" >d:\temp\result.txt")
Parameters go here, change it to your parameters that you want to pass to Command Prompt.
Start Process takes Credential Object hence suggesting this approach.
$params=#("/C";"$x";" >d:\temp\result.txt")
$prog="cmd.exe"
Start-Process -Credential $cred -Verb runas $prog +$params

PowerShell open Internet Explorer and connect to Website with Credentials

Is it possible to create a PowerShell Script that opens Internet Explorer, and connect to an internal IIS Website with credentials?
First part is easy:
$ie = New-Object -Com InternetExplorer.Application
$ie.visible = $true
$ie.navigate("http://mysite")
Now I have no idea how to solve the second part. IE starts and security popup comes up.
Anyone an idea how to handover the credentials? Thanks in advance.
PS: It's an internal IIS Server. I can choose from Basic or Windows Authentication.
If integrated Auth is helpful, how about using Start-Process with the -Credential parameter.
Start-Process -FilePath "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -ArgumentList "http://mysite" -Credential $YourCreds
Or you wrap that to get the correct folder if this will be running on different computers.
if ($arch = $env:PROCESSOR_ARCHITECTURE -eq 'AMD64') {
Start-Process -FilePath "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -ArgumentList "http://mysite" -Credential $YourCreds
}
else {
Start-Process -FilePath "C:\Program Files\Internet Explorer\iexplore.exe" -ArgumentList "http://mysite" -Credential $YourCreds
}

-Windowstyle Hidden not working on Powershell

So I know pretty much nothing about PowerShell but I've been trying to slowly learn.
The end goal is to have PowerShell open up a NOT visible instance of Internet Explorer that navigates to 'Google.com' with the parameters
-noframemerging -private
So far
Start-Process -FilePath 'C:\Program Files\Internet Explorer\iexplore.exe' 'google.com -noframemerging -private'
works in creating an IE but it is NOT hidden.
I also tried another piece of code
Start-Process -WindowStyle Hidden -FilePath 'C:\Program Files\Internet Explorer\iexplore.exe' 'google.com -noframemerging -private'
but the IE window is still visible. How can I fix this?
And -WindowStyle Minimized works.
Start-Process -WindowStyle Hidden iexplore.exe
-ArgumentList "google.com -noframemerging -private"