10

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?

Brett Postin
  • 11,215
  • 10
  • 60
  • 95

6 Answers6

32

Try Import-Module Carbon -Verbose:$false

jbsmith
  • 1,616
  • 13
  • 10
6

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
Scott Gartner
  • 862
  • 14
  • 22
3

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

Arluin
  • 594
  • 1
  • 8
  • 21
2

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 swollen 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.

dustinmoris
  • 3,195
  • 3
  • 25
  • 33
  • 2
    I was unable to get this to work, but `4>$null` did work. Downside is that the output will be completely lost instead of redirected to the debug stream. – Andacious Jan 19 '18 at 15:10
0

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.

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
0

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.