Powershell : Copy a file and Execute it with one command


 I had a requirement to create a single line Powershell command that will completed the following actions:

  1. Copy a script from a Central server location
  2. Copy that file to a local folder that already exists on that device
  3. Execute that file that has been copied
This needed to be done with one command, this means it could be used as a rule for a VPN service provider policy, but works outside of the VPN situation as well, however this needed to be completed with no dialogue boxes, no popups, no profile loads and no window that is visible.

This is what I have been using to complete the job:

Powershell.exe -ExecutionPolicy Bypass -NoProfile -NoLogo -NonInteractive -Windowstyle Hidden -Command "& {Copy-Item -Path '\\smbsahre\Scripts\RegistryChecker.ps1' -Destination 'C:\temp\' -Force; & 'C:\temp\RegistryChecker.ps1'}"

The script that runs will write something to a network share, here we can see that share with nothing in the folder which is correct:



Lets get the script ready in Powershell like this:


When we press enter to execute the Powershell window should vanish and we should see a file in the network share folder with the results.....

Excellent, that is exactly we need, now this can be rolled out to more devices for automatic reporting, hopefully this helps someone else.


Previous Post Next Post

نموذج الاتصال