Suppressing VERBOSE for Import-Module
Asked Answered
A

6

11

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?

Alathia answered 20/3, 2014 at 15:36 Comment(0)
S
34

Try forcing the -Verbose parameter to $false:

Import-Module Carbon -Verbose:$false
Silda answered 20/3, 2014 at 15:48 Comment(0)
P
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
Plaice answered 14/2, 2015 at 3:38 Comment(0)
M
3

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

Maxama answered 20/3, 2014 at 15:57 Comment(0)
V
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 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.

Vivianna answered 11/3, 2015 at 11:17 Comment(1)
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.Lapstrake
S
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.

Snail answered 20/3, 2014 at 16:11 Comment(0)
R
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.

Rawdon answered 15/2, 2021 at 19:3 Comment(3)
please add some text explaining WHY your code will help ... plain code dumps are not considered proper. [grin]User
An I was so excited to have my first contribution. I'll edit it shortly.Rawdon
i'm looking forward to it ... [grin]User

© 2022 - 2024 — McMap. All rights reserved.