3

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!

2
  • What are the errors and can you show us the pertinent part of the script you are running. Is your non-standard module sourced in your script? Commented Jul 19, 2014 at 3:38
  • Don't "run the ISE from the command line". Fix your script so that it runs properly regardless of where you run it. Commented Jul 20, 2014 at 15:09

2 Answers 2

4

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
.....
Sign up to request clarification or add additional context in comments.

2 Comments

This almost did it! Since it was not a standard module, just using the name of the module (e.g., Import-Module ActiveDirectory) doesn't work. First I used Get-Module -ListAvailable to find the path of the module, then use Import-Module C:\path\ActiveDirectory and it worked.
:) As longs as the modules have been installed in a module location, Import-Module ModuleName should work. You could also do Get-Module -Listavailable ActiveD* | Import-Module
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.