Import-PSSession fails in script, works in shell Import-PSSession fails in script, works in shell powershell powershell

Import-PSSession fails in script, works in shell


Your script works fine on it's own and also when called from within another script:

& 'C:\Scripts\ManageUser.ps1' -disableUser -username "foo"Get-Mailbox -resultsize 5 | ConvertTo-Csv -NoTypeInformation | Out-File C:\Scripts\mytest.csv

Subsequent runs will get the import error/warning due to not having -allowclobber as mentioned by @SamuelWarren...

In your case (at least when initially writing the question long ago) the error was due to another variable that you haven't mentioned here. It's likely you fixed that after the first run, and then subsequent tests were showing the AllowClobber error.

Also worth noting (for anybody who comes across this in the future) to check the path of the file you are calling. Just because you try to use a relative path .\myfile.ps1 doesn't mean that PowerShell is currently looking in the right directory.

Check: $psscriptroot

Or Split-Path -Path $($global:MyInvocation.MyCommand.Path)