How to use a Function in Powershell

I have a script I am using from Kris Powell. To install printers. From what I can see I need to call these functions before it will actually install a printer via IP Address. any ideas on how i do this?
Here is what I have

$PrinterIP = “192.168.141.20”
$PrinterPort = “9100”
$PrinterPortName = “IP_192.168.141.20” + $PrinterIP
$DriverName = “Ricoh Aficio MP C6000 PS”
$DriverPath = “\\bh-miworks-srv2\PrintDrivers\Ricoh MP c6501 e7200 Dockside Color\Win 10 PS\English\Prntdrvr\Ps_drvr\Win_2K_XP_VISTA”
$DriverInf = “\\bh-miworks-srv2\PrintDrivers\Ricoh MP c6501 e7200 Dockside Color\Win 10 PS\English\Prntdrvr\Ps_drvr\Win_2K_XP_VISTA\oemsetup.inf”
$PrinterCaption = “BH DockSide Color”

Function CreatePrinterPort {
param ($PrinterIP, $PrinterPort, $PrinterPortName, $ComputerName)
$wmi = [wmiclass]”\\$ComputerName\root\cimv2:win32_tcpipPrinterPort”
$wmi.psbase.scope.options.enablePrivileges = $true
$Port = $wmi.createInstance()
$Port.name = $"192.168.141.20"
$Port.hostAddress = $"192.168.141.20"
$Port.portNumber = $9100
$Port.SNMPEnabled = $false
$Port.Protocol = 1
$Port.put_
}

Function InstallPrinterDriver {
Param ($DriverName, $DriverPath, $DriverInf, $ComputerName)
$wmi = [wmiclass]”\\$ComputerName\Root\cimv2:Win32_PrinterDriver”
$wmi.psbase.scope.options.enablePrivileges = $true
$wmi.psbase.Scope.Options.Impersonation = `
[System.Management.ImpersonationLevel]::Impersonate
$Driver = $wmi.CreateInstance()
$Driver.Name = $"Ricoh Aficio MP C6000 PS"
$Driver.DriverPath = $"\\bh-miworks-srv2\PrintDrivers\Ricoh MP c6501 e7200 Dockside Color\Win 10 PS\English\Prntdrvr\Ps_drvr\Win_2K_XP_VISTA"
$Driver.InfName = $"\\bh-miworks-srv2\PrintDrivers\Ricoh MP c6501 e7200 Dockside Color\Win 10 PS\English\Prntdrvr\Ps_drvr\Win_2K_XP_VISTA\oemsetup.inf"
$wmi.AddPrinterDriver($Driver)
$wmi.Put_
}

Function CreatePrinter {
param ($PrinterCaption, $PrinterPortName, $DriverName, $ComputerName)
$wmi = ([WMIClass]”\\$ComputerName\Root\cimv2:Win32_Printer”)
$Printer = $wmi.CreateInstance()
$Printer.Caption = $"BH DockSide Color"
$Printer.DriverName = $"Ricoh Aficio MP C6000 PS"
$Printer.PortName = $"192.168.141.20"
$Printer.DeviceID = $"DockSide Color"
$Printer.Put_
}

Output is as follows

PS C:\windows\system32> C:\Users\christnerw\Desktop\printer test.ps1

PS C:\windows\system32> 

So basically I am not getting any errors (Score)

Thanks

So executing that script is going to define those functions in a new scope and then it will immediately go away once the script is finished running. To actually be able to execute the functions after this script runs you could use the “dot-sourcing” method which would define these functions in your current PowerShell scope, at that point you could actually run the functions.

Rather than having me explain this check out this post by Ed Wilson where he explains this:

Thanks for the info I will check out the link

Another way to go is you can actually save that file as a .psm1 then simple import-module file.psm1 and that will load the functions into the current context if you wish to run it in your current console. Nice easy way to test your functions to make sure they behave as you expect. In WMF5, you’ll get a warning as the functions don’t follow the proper format but they will work (CreatePrinterPort should be New-PrinterPort, for example). You can use Get-verb for the list of “approved verbs”, but you can ignore this list if the function will only exist within other scripts (not meant for console consumption)

If you ultimately want this in a login script or something else, follow Nick’s link, or place the final script at the end of the code you started (if you don’t need these functions to exist outside the final script).

wchristner: I don’t get completely why you post this question. I thought it was already answered with this: https://powershell.org/forums/topic/function-createprinter/#post-37455

You are basically asking again the same question.