Aller au contenu

Les Fonctions

Fonction avec retour de valeur

function NomFonction {
   param (
    paramètre1, paramètre2
  )
   commande qui met en oeuvre les paramètres
}
Appel de la fonction : NomFonction -paramètre1 valeur1 -paramètre2 valeur2

function Addition {
    param (
        [int]$nb1, [int]$nb2
    )
    $nb1 + $nb2
}
# Appel de la fonction
$Resultat = Addition -nb1 10 -nb2 20

Fonction avec affichage graphique (msgbox)

function MsgBox {
    param (
        [String]$Message, [String]$Titre
    )
    $WsShell = New-Object -ComObject wscript.Shell
    $WsShell.PopUp($Message, 0, $titre)
}

MsgBox -Message "Bienvenue sur PowerShell" -Titre "Message PowerShell"