How to run a .ps1 (powershell script) on all domain workstations?

I have a power shell script to display a pop up window (alert message) and it works fine on local Pc or when run from command prompt or powershell itself but I am not able to run/deploy the same using sccm or even usinbg task schedular (GPO).

Is there a way to invoke the powershell script that sits on network share or local workstation onto all domain workstations.

Who is Participating?
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
Maybe you're running it bad:

Or just doesn't work.
What you can do is to get the online computers within the subnet, save it into a variable like $computers, and then use a foreach computer in online array running the invoke-command - computer $computer -scriptblock $scriptblockvar -credentials $creds (-asjob -jobname "jobname)
() are optional parameters, the credentials $creds=Get-Credential (or automate it saving username and password within the ps1 file).

You can run it from a centralized computer (like a server and use scheduler, as shown in my article).
DhattksAuthor Commented:
thanks i will try this
Shaun VermaakTechnical Specialist/DeveloperCommented:
Or as a Logon or StartUp script
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

DhattksAuthor Commented:
Hi .
Below is my script and I do not want to run as startup script or on logon. I would like to push / deploy on demand.
Jose > where would I add the $computers variable and invoke command. Also do I need to provide credentials $creds and where?
Sorry I am new to this.
I appreciate your help.

function New-PopupMessage {
# Return values for reference (
# Decimal value    Description  
# Define Parameters
        # The popup message
        # The number of seconds to wait before closing the popup.  Default is 0, which leaves the popup open until a button is clicked.
        [int]$SecondsToWait = 0,
        # The window title
        # The buttons to add
        # The icon type
# Convert button types
        "Ok" { $Button = 0 }
        "Ok-Cancel" { $Button = 1 }
        "Abort-Retry-Ignore" { $Button = 2 }
        "Yes-No-Cancel" { $Button = 3 }
        "Yes-No" { $Button = 4 }
        "Retry-Cancel" { $Button = 5 }
        "Cancel-TryAgain-Continue" { $Button = 6 }
# Convert Icon types
        "Stop" { $Icon = 16 }
        "Question" { $Icon = 32 }
        "Exclamation" { $Icon = 48 }
        "Information" { $Icon = 64 }
# Create the popup
(New-Object -ComObject Wscript.Shell).popup($Message,$SecondsToWait,$Title,$Button + $Icon)
# Close the Task Sequence Progress UI temporarily (if it is running) so the popup is not hidden behind
        $TSProgressUI = New-Object -COMObject Microsoft.SMS.TSProgressUI
Catch {}
# Define the parameters.  View the function parameters above for other options.
$Params = @(
    "Good day All,

Please note that on Friday 06 Oct 2017 starting at 13:00, MCE CS will be applying Arcgis Desktop 10.4.1 patches on all MCE production unclassified computers.

Users are asked to log off but do not shutdown their MCE computers before leaving. For those who work pass 13:00, please follow pop-up instructions on your computer related to this update.


Veuillez noter que vendredi le 06 Oct 2017 à partir de 13h00, MCE CS appliquera les patches de Arcgis Desktop 10.4.1 sur les ordinateurs du réseau MCE non-classifié.

Les utilisateurs sont invités à se déconnecter, mais ne pas éteindre leurs ordinateurs MCE avant de partir. Pour ceux qui travaillent au-delà de 13h00, veuillez suivre les instructions concertantes apparues à l’écran de l’ordinateur.

Thank you.
MCE IT Department
." # Popup message
    0                           # Seconds to wait till the popup window is closed
    "MCE IT / CS: Message of The Day" # title
    "Ok"                        # Button type
    "Exclamation"               # Icon type
# Run the function
New-PopupMessage @Params
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You can either put that code into a PS1 script on a network resource you can reach from each PC, or  store it into script block like
$script = {
  function New-PopupMessage {
  # and so on
  New-PopupMessage @Params

Open in new window

Then invoke that against all machines:
$cred = Get-Credentials
Get-ADComputer | % {
  invoke-cmd -ComputerName $_.Name -ScriptBlock $script -Credential $cred -AsJob
  # or with a script:
  # invoke-command -ComputerName $_.Name -FilePath '\\server\share\script.ps1' -Credential $cred -AsJob

Open in new window

Since the popup waits for confirmation, you'll have an issue - execution is halted. Hence I used the internal job system, running the popup in a background task. But be aware there is a limit of background tasks running simultanously, so script execution might halt at say 64 PCs, until some popups are confirmed.
We could also use the in-built parallel execution feature of Invoke-Command, but it has the same issue:
$cred = Get-Credentials
invoke-cmd -ComputerName (Get-ADComputer).Name -ScriptBlock $script -Credential $cred -ThrottleLimit 256
# or with a script:
# invoke-cmd -ComputerName (Get-ADComputer).Name -FilePath '\\server\share\script.ps1' -Credential $cred -ThrottleLimit 256

Open in new window

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Correct suggestions in this context are #a42412341 and #a42412975.
Shaun VermaakTechnical Specialist/DeveloperCommented:
and what is wrong with #a42412647?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Shaun, that requires an action by the client (starting the machine or logging in), versus a push as the OP wants (and that can be read from the question, though not 100% clear). The comment #a42412929 (posted later than your comment) clarifies the push requirement.
It is a gray area whether to include your comment or not.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.