0
votes

I'm trying to create a shortcut to run my PowerShell script. This works fine but not when it needs to load the Active directory module. I've searched everywhere, but couldn't find an answer. When adding the line 'import-module' it doesn't work anymore:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command import-module ActiveDirectory "& 'C:\MyScript.ps1' "

Thank you for your help.

2
You probably want to add a #Requires -Module ActiveDirectory line in your script.brianary

2 Answers

2
votes

You're missing a semicolon. At the moment you're trying to execute the literal line

import-module ActiveDirectory & 'C:\MyScript.ps1'

which makes no sense if you try it interactively. It makes no more sense from the command line.

Try

-Command "&{Import-Module ActiveDirectory; & 'C:\MyScript.ps1'}"

instead.

0
votes

If you always want use ActiveDirectory you can put import-module ActiveDirectory command in your $profile file.