How does one import module inside the cmd? - powershell

Currently, I'm using this,
cd Images powershell.exe & Import-Module .\Resize-Image.psm1 & Resize-Image ....
What this does in PS, is that it will import the module and I'm able to use it's function to rescale my pictures and output it, it works pretty well in PS but i'm intending to use cmd prompt to call this as my other lines of codes are in cmd prompt.
When i use this in cmd, it just displays
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
and is waiting for a new command even though the above code is in a .bat file.

The code presented starts PowerShell, but has no command for it. After the AMPERSAND, the next command is Import-Module which is unknown to cmd.exe.
PowerShell commands can be separated by a SEMICOLON character.
cd Images
powershell.exe -NoLogo -NoProfile -Command ^
"Import-Module .\Resize-Image.psm1; Resize-Image ...."
If you could run in PowerShell:
Set-Location './Images' # probably should have a fully-qualified path
Import-Module './Resize-image.psm1'
Resize-Image ...

Related

Start new shell not working when the ps1 has become executable with ps2exe

I am trying to create an executable (.exe) at windows that will perform some actions at first, and then open an interactive shell. In my case the interactive shell I want to be a wsl, but the problem is shown with every type of shells created.
For example:
Supposing that we have the file
test.ps1
Write-Host "Hello from my test program"
Invoke-Expression "powershell"
If I run it with the command: ./test.ps1, the outcome will be to print the message and start a new powershell instance.
If i run the command: Invoke-ps2exe .\test.ps1 test.exe, and then run the test.exe, the outcome will be:
Hello from my test program
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
but I cannot write anything at the window.
Can anyone explain me why this is happening, and how I could overcome this?
Modify your test.ps1 file as follows:
Write-Host "Hello from my test program"
Start-Process -Wait -NoNewWindow powershell.exe
This change ensures that PowerShell's usual interactive host, ConsoleHost, in the newly launched PowerShell session launched from your script doesn't run on top of the simplified host that is built into the .exe files that ps2exe generates, which appears to cause your problems.

PSExec: cmd exited on [COMPUTER] with error code -196608

I've been working on trying to run a Powershell script on the network using PSExec, but every time I run it, I get this message:
PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com
Couldn't access [COMPUTER]:
The filename, directory name, or volume label syntax is incorrect.
C:\PSTools> psexec \\[COMPUTER] /s cmd /c %SystemRoot%\system32\WindowsPowerShell\
v1.0\powershell.exe -ExecutionPolicy Bypass -file c:\apps\test.ps1
PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com
The argument 'c:\apps\test.ps1' to the -File parameter does not exist. Provide
the path to an existing '.ps1' file as an argument to the -File parameter.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
cmd exited on [COMPUTER] with error code -196608.
The goal of the script is to traverse domain-connected laptops and save a specific list of directories to a file on my computer. That script is as follows:
powershell.exe -command "& {& get-childitem 'c:\users\*\appdata\local\google\chrome\user*data\default\extensions\*'}" > \\[MY_COMPUTER]\C$\users\[USERNAME]\desktop\Chrome_Extensions.txt
I am calling this script with this command:
psexec \\[COMPUTER] /s cmd /c %SystemRoot%\system32\WindowsPowerShell\
v1.0\powershell.exe -ExecutionPolicy Bypass -file c:\apps\test.ps1
When I run the script without psexec \\[COMPUTER] /s , it runs fine and saves the correct information about the computer I'm running it on, then sends the information to my computer's desktop. But running it with the aforementioned line causes the error above, and I can't target machines on the network. So, what about the PSExec command causes this to error out, as it doesn't seem like there's really a lot to it? Perhaps I'm misunderstanding, and assuming that PSExec should run the same as PowerShell, which very well might not be the case. I guess I'm just a little lost on how to use it, and any assistance would be greatly appreciated! Thank you!
With help from #Briantist, I've been able to run the command Enable-PSRemoting on the target computer, then run the script with invoke-command -computername [COMPUTER] -filepath c:\apps\test.ps1
Using PSExec was just too messy, so I'm glad this was able to work another way. Now to figure out how to enable remoting on all computers on the domain without connecting to all of them individually and doing it manually...

how to exec ps1 file from cmd?

I have a ps1 file, Test.ps1, which I need to exec from cmd. For test purposes this file only has 1 line:
write "ps1 test successful"
I was trying to exec this ps1 file from cmd. I googled and it seemed that including the following line might help:
Set-ExecutionPolicy RemoteSigned
write "ps1 test successful"
However I still can't exec this test. I've tried:
powershell Test
powershell Test.ps1
Test
Test.ps1
The cmd path context is set to the dir in which the ps1 script resides. Any idea what I might be doing wrong here?
Does this work?
Powershell -ExecutionPolicy Bypass -File .\Test.ps1
I've done this before with a .bat file, and this was the syntax used. In this instance, you're running from within the same directory as the powershell script (otherwise adjust the filename argument as necessary). And you may need to be running the CMD prompt as admin, if you aren't already.
Use
powershell.exe -ExecutionPolicy Bypass -File "C:\dir name\test.ps1"
Of course, replace C:\dir name\test.ps1 with the path and filename of the script you want to run, enclosed in " (double quotes).
Alternatively, start PowerShell in its own window, then run the script.
On macOS:
Use Homebrew to install Powershell:
brew install --cask powershell
Switch to Powershell:
pwsh
Execute the script:
./Test.ps1
My PowerShell script (Test.ps1):
echo "trying to test something"
I can execute it in cmd with this command:
.\Test.ps1
My output:
trying to test something

put a powershell script on path through powershell commands.

I have a powershell script that I want to run from cmd/ps any location by putting it in path.
What is the command that can achieve that ?
I m basically looking for a UNIX equivalent of putting your script in bashrc and thus available from anywhere to run.
echo 'export PATH=$PATH:/path/to/script' >> ~/.bashrc && source ~/.bashrc
In windows you also have the system variable PATH that's used for defining where to locate executables.
You could do the following that should be equivalent assuming you're only using Powershell:
$newPath = "c:\tmp\MyScriptPath";
[Environment]::SetEnvironmentVariable('PATH', "$($env:Path);$newPath", [EnvironmentVariableTarget]::User);
# Update the path variable in your current session; next time it's loaded directly
$env:Path = "$($env:Path);$newPath";
You can then execute your script directly in Powershell with just the name of the script.
However_ : this will not work under cmd because cmd doesn't know how to handle the ps1 script as an executable. Normally one would execute the script from cmd by calling the following:
Powershell.exe -executionpolicy remotesigned -File C:\Tmp\Script.ps1
If this is "unacceptable" for you, the easiest way is to create a bat script along with your ps1 script (same path) and add the following content :
Script.bat (Assuming you have Script.ps1 in the same folder):
#ECHO OFF
PowerShell.exe -Command "& '%~dpn0.ps1'"
PAUSE
This will create the wrapper needed to Invoke Script anywhere in your cmd as batch files can be executed from cmd

How to execute a PowerShell script from Notepad++

I am using Notepad++ to edit a PowerShell file and want to be able to execute that file from inside Notepad++.
How can I set that up?
It took me a little fiddling, but I finally got this working. (I am using version 1.0 but this should work in other versions as well.)
Notepad++ can be set up to run commands, and assign shortcuts to those commands, as follows:
From the menu, click Run → Run
Add the command
C:\NotepadRun.bat "$(FULL_CURRENT_PATH)"
Save the command, giving it a name and a key shortcut.
Below are the contents of the batch file. I named mine NotepadRun.bat, but you can name it whatever.
#echo off
GOTO %~sx1
:.ps1
cd "%~d1%~p1"
powershell.exe .\%~n1%~sx1
GOTO end
:.rb
ruby "%~f1"
GOTO end
:.php
php "%~f1"
GOTO end
:end
pause
As a note upgrading to Windows7 and Powershell 2 I found some Issues with this and have updated to passing in an ExecutionPolicy to ensure I can run the script I am editing.
:.ps1
cd "%~d1%~p1"
powershell -ExecutionPolicy Unrestricted -File "%~n1%~sx1"
GOTO end
See Using Notepad++ to Compile and Run Java Programs and replace "javac" with "C:Windows\system32\WindowsPowerShell\v1.0\powershell.exe" (or your path to PowerShell). (Caveat: I'm not a Notepad++ user and haven't tried this.)
That said, I'd just use PowerShell ISE (installs with PowerShell) or one of the other dedicated PowerShell IDEs instead.
I would recommend using PowerShell ISE which comes as part of PowerShell and designed specifically for Powershell.
You can run a saved script from "Run" -> "Run" menu in Notepad++ with the following command:
powershell.exe -noexit -command . \"$(FULL_CURRENT_PATH)\"
Based on the answers before:
powershell.exe -ExecutionPolicy Unrestricted -NoLogo -File "$(FULL_CURRENT_PATH)"
You can also add the -NoExit parameter to keep PowerShell from closing automatically:
powershell.exe -ExecutionPolicy Unrestricted -NoExit -NoLogo -File "$(FULL_CURRENT_PATH)"
Note: File has to be saved.