Calvin Li
Calvin Li

Reputation: 2684

Run PowerShell ISE from the command line?

So I have a script that works fine when I run it from PowerShell ISE. However, I need to automate it, so I run powershell.exe .\script.ps1, but I get errors about some of the commands not being recognized (they are from a non-standard module).

Any help is appreciated, thanks!

Upvotes: 3

Views: 18236

Answers (2)

JPBlanc
JPBlanc

Reputation: 72610

One reason should be that a script is dot sourced or a module is loaded from your profile script. In this case your problem can come from the fact that starting PowerShell from the command line and starting PowerShell ISE does not use systematicaly the same profile script. Have a look at $Profile var in each one and edit the associated file.

$Profile on my ISE :

C:\Users\JPB\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

$Profile on my command Line :

C:\Users\JPB\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

More about profiles.About_Profile

A way to know all your $Profile paths : enter image description here

Upvotes: 3

Frode F.
Frode F.

Reputation: 54871

Edit the beginning of your script to import all dependencies(modules). This is good practice as it makes the code more readable and works with both PS 2.0 an 3.0+

script.ps1

#Import example module
Import-Module ActiveDirectory

#Script start
$name = Read-Host "Username"
$user = Get-ADUser $name
.....

Upvotes: 4

Related Questions