Format-List not recognized - powershell

I'm trying to create a script to automatically export Outlook contacts based on display name. When I run the .bat in Exchange PowerShell, it tells me that Format-List is an unrecognized command. Not quite sure what's going on here. What I've found online usually just recommends running it from PowerShell.

.bat is an extension for batch files, which are run in CMD, even if you call them from PowerShell. CMD and batch files don't know anything about PowerShell cmdlets. If you want to write a PowerShell script, it must have the extension .ps1.

Related

How to run an executable through PowerShell without batch files or cmd?

I am currently using batch files to run a set of simulations. Each line in the batch file reads:
"filepath\program.exe" "filepath\simulation.file"
The quotation marks exist to bound any spaces that exist within the file paths. Without any spaces in the file paths, the quotation marks can be removed.
When I run the batch file through PowerShell, using the following command, it works fine:
.\batch.bat
The executable is run and the output is written to the host, as if I was running the same batch file in cmd.
However, I want to ditch the batch files and run the command directly through PowerShell. When I run the following, I get the program to execute, though it doesn't run properly and I don't get anything written to host. It also appears to hang until I use Ctrl+C to cancel.
& "filepath\program.exe" "filepath\simulation.file"
Could you please help me with the following?
Any resources discussing how PowerShell executes batch files.
How to run an executable through PowerShell without using cmd or a batch file and have it write to host.
Have you tried Start-Process?
Start-Process -FilePath "filepath\program.exe" -ArgumentList "filepath\simulation.file" -Wait
PS: The Parameter -Wait is optional.
Although I'm not quite sure why yet, I found out that this issue only occurred while working remotely. Whenever I was connected to the network locally the command ran just fine.
Since I plan to execute the command on PCs that are situated locally on the network, I'll leave it at this for now.
Thanks to everyone who commented!

Running a small WMI Powershell Script

I'm trying to have a few scripts that I can map to run from my keyboard for quickly changing the monitor/screen brightness. After some searching on the internet, I found this script which works when I enter it into Powershell.
$monitor=#(gwmi WmiMonitorBrightnessMethods -ns root/wmi)[0]
$monitor.WmiSetBrightness(50,0)
After I saved it as a .ps1 file and tried running it from the file, powershell tells me: The term "path of the file" is not recognized as the name of a cmdlet, function... and so on.
I'm not familiar with Powershell at all, can someone help with what I need to add in order for the script to run properly?
By default you can't run a PowerShell script that is in the current directory without putting .\ in front of the script name, or calling the full path of the script.
This is a security feature.
If you are in the directory that contains the script, run it by executing in a PowerShell window:
.\yourscript.ps1
Where yourscript is the name of your script.
See here for more information: https://ss64.com/ps/syntax-run.html
You may also see this error if your script has spaces in its name. If that is the case, enclose the path in quotes:
.\'your script.ps1'

Execute any file as powershell script

I encountered a challenge that I failed to resolve the way I wanted it to do.
I got a file that contains a powershell script, but that file does not have the extension assigned to powershell. The question is: How can I execute a powershell in a script file with the wrong file extension (or none)?
Invoke-Expression does not seem to work because it always executes the default action assigned to the file type. If I give that cmdlet a *.txt file the editor pops open.
I know that I can resolve that by renaming the script file or naming it properly in the first place. This is what I ended up doing.
Still I wonder if it is possible to execute a file as a script with the wrong file extension without modifying, renaming or coping the file. And if it is not working… why is that?
Powershell is designed such that executing or dot sourcing a file requires a .ps1 extension, and Powershell.exe will refuse to run any file that doesn't have that extension.
One way to invoke Powershell code from a non-ps1 file is to launch Powershell.exe using STDIN, and pipe your script to it. This requires a new shell, so is not very good for launching scripts from within an existing scripting environment.
Powershell.exe - < thescript.txt
Another way is to create a temporary .ps1 file and execute that. This has the advantage of using the current scripting environment, but requires a temporary file.
Copy-Item -Path '.\thescript.txt' -Dest '.\temp.ps1'
. .\temp.ps1
del .\temp.ps1
In my opinion, the file extension restriction is silly, but that's how it was designed. Apocryphally, this is for security reasons, but I can find no citation to back it up.
Or you use Get-Content to read the file and then invoke that with Invoke-Expression or Invoke-Command -ScriptBlock.

How to run powershell script that doesn't have ps1 extension

I am using ops tool Rundeck that allows for execution of arbitrary scripts. I enter the script text in the web application widget and upon execution, Rundeck saves it to temporary file and calls interpreter. The problem is that the temporary file doesn't have ps1 extension and Powershell refuses to execute it.
Is there any way to set up Powershell to ignore extension ?
=== EDIT 2018 ===
Rundeck now has an option for this within a job definition.
I know I'm not strictly answering your questions regarding setting up PowerShell to ignore extensions, but here is one way of executing code from a textfile:
Invoke-Command -ScriptBlock ([ScriptBlock]::Create((Get-Content .\file.txt)))
This reads the contents of file.txt and converts it into a scriptblock, before executing it using Invoke-Command.

Powershell.exe running the script in cli, or a wrapper?

I have a third-party application that's extensible by adding exe-files that perform dataconversion etc. I've written an extension in Powershell that does the conversion I want, but I'm unable to get the third-party app to run my ps1, as it will only accept an .exe file as an extension.
The app adds a filename as the first (and only) commandline argument to the extension, so the command it runs looks like:
someprogram.exe somefile.xml
I tried to get it to run Powershell.exe with my script as an argument, but I haven't been able to figure out how and if that's possible. Some stuff I tried like
powershell.exe myscript.ps1
won't work. I tried getting the script to find the correct XML file itself, but still somehow I couldn't get Powershell to run off the commandline and take a script as an argument and run it.
Next I thought about writing a small .exe file that only runs the Powershell script, but is that even possible? If it is, could someone nudge me in the right direction?
Powershell wants to have a qualified path to script files before it will run them. So you need to either use
powershell.exe .\myscript.ps1
if it lies in the current working directory (unlikely and prone to break for this use case) or use the full path to the script:
powershell.exe C:\Users\Foo\Scripts\myscript.ps1
or similar.
You can also try Powershell.exe -Command "& your-app.exe ${your-arguments}