check if pssnapin could be load

Hello all,
I have a multi form project. The mainform has two buttons. In the formload event I like to check when the exchange snapin is installed on the machine show the exchange button, when not hide it. I tried diffrent code, but all what i tried was not successfull. I tested it on the exchange server directly. Hope someone has an idea.
Here’s the code I have at the moment:

	if (-not (Get-PSSnapin -Registered | Where-Object{$_.Name -eq "Microsoft.Exchange.Management.PowerShell.SnapIn"}))
		$buttonExchange.Visible = $false
		$buttonExchange.Visible = $true


When you say it is not working, is like the button is visible even if the snapin is not available ? If feasible, can you share the whole code ? you can replace secret values with dummy values.

Have you exposed the $buttonExchange variable to a scope where your if statement will have access?

$[Global]buttonExchange = …


$[Script]buttonExchange = …


thanks for your replies. If i set both to $true, it is displayed. The snapin will be loaded with another form. I only want to prevent, when the script runs on an other system without the exchange snapin is installed that it is not possible to click it. I’m using PowerShell Studio from SAPIEN. Modified the code since my last post but also no luck. I don’t post the other two functions “CheckExport” and “Checkxml” because this is working.


#region function Check-Exchange-Modul
function CheckExchangeModul
$buttonExchange.Visible = [bool](Get-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn -ea 0)

# if (-not (Get-PSSnapin -Registered | Where-Object{$_.Name -eq “Microsoft.Exchange.Management.PowerShell.SnapIn”}))
# {
# $buttonExchange.Visible = $false
# }
# else
# {
# $buttonExchange.Visible = $true
# }

#endregion function Check-Exchange-Modul

#region SplashScreen_Load
$formSplashScreen_Load = {
Use the -PassTru parameter to update the splash screen text:
$splashForm = Show-SplashScreen … -PassThru
#Update the splash screen text
$splashForm.Text = ‘Loading Modules…’

$paramShowSplashScreen = @{
Image = $pictureboxSplashScreenHidden.Image
PassThru = $false
Timeout = 2

Show-SplashScreen @paramShowSplashScreen

#endregion SplashScreen_Load

#Append the event to the form

#region Splash Screen Helper Function
function Show-SplashScreen
Displays a splash screen using the specified image.

Mandatory Image object that is displayed in the splash screen.

(Optional) Sets a title for the splash screen window.

The amount of seconds before the splash screen is closed.
Set to 0 to leave the splash screen open indefinitely.
Default: 2

.PARAMETER ImageLocation
The file path or url to the image.

Returns the splash screen form control. Use to manually close the form.

The splash screen will hold up the pipeline until it closes.

PS C:\> Show-SplashScreen -Image $Image -Title ‘Loading…’ -Timeout 3

PS C:\> Show-SplashScreen -ImageLocation ‘C:\Image\MyImage.png’ -Title ‘Loading…’ -Timeout 3

PS C:\> $splashScreen = Show-SplashScreen -Image $Image -Title ‘Loading…’ -PassThru
#close the splash screen

Created by SAPIEN Technologies, Inc.

The size of the splash screen is dependent on the image.
The required assemblies to use this function outside of a WinForms script:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[Parameter(ParameterSetName = ‘Image’,
Mandatory = $true,
Position = 1)]
[Parameter(Mandatory = $false)]
[int]$Timeout = 2,
[Parameter(ParameterSetName = ‘ImageLocation’,
Mandatory = $true,
Position = 1)]

#Create a splash screen form to display the image.
$splashForm = New-Object System.Windows.Forms.Form

#Create a picture box for the image
$pict = New-Object System.Windows.Forms.PictureBox

if ($Image)
$pict.Image = $Image;

$pict.AutoSize = $true
$pict.Dock = ‘Fill’

#Display a title if defined.
if ($Title)
$splashForm.Text = $Title
$splashForm.FormBorderStyle = ‘FixedDialog’
$splashForm.FormBorderStyle = ‘None’

#Set a timer
if ($Timeout -gt 0)
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = $Timeout * 1000
$timer.Tag = $splashForm

#Show the form
$splashForm.AutoSize = $true
$splashForm.AutoSizeMode = ‘GrowAndShrink’
$splashForm.ControlBox = $false
$splashForm.StartPosition = ‘CenterScreen’
$splashForm.BackColor = ‘AliceBlue’
$splashForm.TopMost = $true

if ($Modal) { $splashForm.ShowDialog() }
else { $splashForm.Show() }

if ($PassThru)
return $splashForm


Although it wont hide the button, setting the Enabled property to $False should ghost/disable it.

i found the mistake. the correct code should be:

$buttonExchange.Visible = [bool](Get-PSSnapin -registered Microsoft.Exchange.Management.PowerShell.SnapIn -ea 0)