Brett Postin
Brett Postin

Reputation: 11375

Suppressing VERBOSE for Import-Module

I'm importing Carbon into my PowerShell script; however when running my script with -Verbose, Carbon also outputs a lot of VERBOSE statements.

Is it possible to Import-Module silently such that I can ignore the verbose statements in the imported module and leave just my own?

Upvotes: 11

Views: 23274

Answers (6)

dustinmoris
dustinmoris

Reputation: 3361

I think a better solution than the one which is marked here
is to redirect the verbose output to a different stream.
This way you can print the output if you need it and it doesn't get lost for ever:

Import-Module Carbon 4>&5

This redirects the verbose stream (4) to the debug stream (5).

When you run your script with the -Verbose switch, it will not output the verbose lines from Import-Module, but you can bring it back by running your script with the -Debug switch.

Upvotes: 3

jbsmith
jbsmith

Reputation: 1666

Try forcing the -Verbose parameter to $false:

Import-Module Carbon -Verbose:$false

Upvotes: 34

spiffykat404
spiffykat404

Reputation: 11

First contribution, I hope this helps.


ipmo $dir\$i 3>$null

ipmo: Short-hand/alias for Import-Module

3>$null: Redirect warnings messages to null

Edit: I wanted to share a table I found at work while I was looking for the best way to help explain this... But I cant find it now. However, the quick an dirty is you may have already noticed that ipmo doesn't act like the rest of those PWshell cmdlet. It has a total of 4 streams in the bg.

I don't remember 1 and 2. 3 is warning. 4 though, 4 is not necessarily error, but it is error at the same time? I don't remember the exact wording. It is something that bypasses errors.

If I do find that award winning table again I'll be sure to share the blessing.

Upvotes: 0

Scott Gartner
Scott Gartner

Reputation: 872

I could not get the solutions above to work with all modules (I'm using Powershell 4.0). This is the solution I ended up using and so far it has worked with every module I've used:

At the top of my script file I have this, to make the -Verbose work for the script (the script has no parameters):

[CmdletBinding()]
Param()

Then when I'm ready to import the modules, I do this:

$SaveVerbosePreference = $global:VerbosePreference;
$global:VerbosePreference = 'SilentlyContinue';

Import-module "Whatever";

$global:VerbosePreference = $SaveVerbosePreference;

Then I just call the script like so:

PowerShell -file something.ps1 -Verbose

Upvotes: 6

JPBlanc
JPBlanc

Reputation: 72630

As Carbon seems to be a script module, can you try to set the $script:VerbosePreference (or just $VerbosePreference) to 'SilentlyContinue' inside the module itself carbon.psm1. The module scope should do the trick.

Upvotes: 0

Arluin
Arluin

Reputation: 594

Import-Module Carbon -Verbose:$false | Out-Null

Upvotes: 3

Related Questions