My script doesnt use the “Switch” and just asks me the parameters, and i couldnt figure out why.
function t-t{
[cmdletBinding()]
Param(
[string] $HoL = (Read-Host ‘1)Benutzer Hinzufügen “n" 2)Benutzer Löschen "
n” 3)Gruppe Hinzufügen “n" 4)Gruppe Löschen "
n” 5) Benutzer in eine Gruppe hinzufügen “`n” 6) Benutzer aus einer Gruppe Entfernen’),
[string] $Benutzer = (Read-Host ‘Benutzernamen eingeben’),
[string] $Gruppe = (Read-Host ‘Gruppennamen eingeben’),
[string] $Password = (Read-Host ‘Password eingeben’),
[string] $BenInGru = (Add-LocalGroupMember $Gruppe $Benutzer),
[string] $BenAusGru = (Remove-LocalGroupMember $Gruppe $Benutzer),
[string] $SuchG = (Get-LocalGroup -Filter {Name -eq $Gruppe}) ,
[string] $SuchB = (Get-LocalUser -Filter {Name -eq $Benutzer}),
[string] $HinzuG = (New-LocalGroup $Gruppe),
[string] $HinzuB = (New-LocalUser $Benutzer -Password $Password ),
[string] $EntfG = (Remove-LocalGroup $Gruppe),
[string] $EntfB = (Remove-LocalUser $Benutzer)
)
switch($HoL)
{
1 {write-host “ja”;$HinzuB;break;}
2 {write-host “ja”;$EntfB;break;}
3 {write-host “ja”;$Gruppe;$HinzuG;break;}
4 {write-host “ja”;$EntfG;break;}
5 {if ($SuchG ) {}
else{$HinzuG};
if ($SuchB ) {}
else{$HinzuB};
$BenInGru;break;}
6 {$BenAusGru;break;}
default{exit}
}
}
t-t
4 Spice ups
Don’t use a read host in your parameter definition. That goes against defining a parameter.
1 Spice up
and what could i use instead?
If you want to pass the parameters to a function instead of it asking for manual input do something like this.
[CmdletBinding()] # Add cmdlet features.
Param (
# Define parameters below, each separated by a comma
[Parameter(Mandatory=$True)]
[int]$DemoParam1,
[Parameter(Mandatory=$False)]
[ValidateSet('Alpha','Beta','Gamma')]
[string]$DemoParam2,
# you don’t have to use the full [Parameter()] decorator on every parameter
[string]$DemoParam3,
[string]$DemoParam4, $DemoParam5
# Add additional parameters here.
)
This is but a sample so you might have to call the Google for more. And sorry that read host is also possible. Didn’t know that. I’m also still learning.
It has to ask for input for
$Hol $Benutzer $Gruppe $Password
The problem i have is that it doesnt go to the switch, it asks for those 4 and then gives me an error because the group i write doesnt exist. but i dont know why it does not go to the switch.
Try and put " before and after the numbers like this. Again I’m also still learning.
switch($HoL)
{
" 1" {write-host “ja”;$HinzuB;break;}
“2” {write-host “ja”;$EntfB;break;}
“3” {write-host “ja”;$Gruppe;$HinzuG;break;}
“4” {write-host “ja”;$EntfG;break;}
“5” {if ($SuchG ) {}
else{$HinzuG};
No problem i am learning too and thanks for your time, but it still doesnt work. And i cant find anything in the internet.
Try and count the ( and ) in this line. They don’t match. Try and copy the $HoL line into the console and execute it without the [string] . See if that pops an error.
[string] $HoL = (Read-Host '1)Benutzer Hinzufügen "`n" 2)Benutzer Löschen "`n" 3)Gruppe Hinzufügen "`n" 4)Gruppe Löschen "`n" 5) Benutzer in eine Gruppe hinzufügen "`n" 6) Benutzer aus einer Gruppe Entfernen'),
Try a simpler read host with not so much comments. Ug forgot the - in Read-Host. Typo
. I’m on a smartphone and can’t edit correct it.
$hol=readhost("enter number")
nope, it doesnt jumpt to the Switch
Read host has to be in ()
Can you post the error then?
traslated in english below
Add-LocalGroupMember : Das Argument für den Parameter “Name” kann nicht überprüft werden. Das Argument ist NULL
oder leer. Geben Sie ein Argument an, das nicht NULL oder leer ist, und führen Sie den Befehl erneut aus.
In C:\Users\Admin\Desktop\sdf.ps1:13 Zeichen:44
- [string] $BenInGru = (Add-LocalGroupMember $Gruppe $Benutzer),
-
- CategoryInfo : InvalidData: (
[Add-LocalGroupMember], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.AddLocalGroupMembe
rCommand
Add-LocalGroupMember : The Argument for the Parameter “Name” cant be checked. The argument is NULL (Zero) or empty, Give an argument that not NULL or empty and retry
- [string] $BenInGru = (Add-LocalGroupMember $Gruppe $Benutzer),
-
- CategoryInfo : InvalidData: (
[Add-LocalGroupMember], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.AddLocalGroupMembe
rCommand
tfl can you se the error cause I can’t without glasses 
@thomaslee
DoctorDNS
(DoctorDNS)
15
it means that a variable is null
To resolve this,. set a breakpoint on the line:
(Add-LocalGroupMember $Gruppe $Benutzer)
Then run the script until it hits the breakpoint and look at the values of these two variables - one is null…
As to why - you need to look at what data the user entered!!
2 Spice ups
How do i do that?
Then run the script until it hits the breakpoint and look at the values of these two variables - one is null…
As to why - you need to look at what data the user entered!!
And i want to create a new user (1 in switch) and it still asks me for the parameters to add a user in a group.
shelly
(Shelly3360)
17
To debug a script, you can either run the script line by line, or add breakpoints at the line or a couple above where you think a problem might be. Both are done using PowerShell ISE. To add breakpoints, click on the line where you want the breakpoint to be and press the F9 key (or you can click on the Debug menu and then Toggle Breakpoint). Run the script by pressing the F5 key (or click Debug then Run/Continue). With the script paused at a breakpoint, you can find the current value of a variable by typing it down in the Command Window.
To run a script line by line, after a breakpoint press the F11 key (Debug → Step Into). To continue the script to the next breakpoint press F5 again.
shelly
(Shelly3360)
18
Welcome to SpiceWorks!
To make reading script easier, please use the ‘Insert Code’ button. Thank you!

First, use a proper IDE or editor with syntax highlighting. The PowerShell ISE is adequate but I personally prefer Visual Studio Code with the PowerShell module.
It helps a lot when learning or troubleshooting.
Second, please use the insert code button when posting scripts. This is what your code looks like when formatted in VS Code:
function t-t {
[cmdletBinding()]
Param(
[string] $HoL = (Read-Host '1)Benutzer Hinzufügen "`n" 2)Benutzer Löschen "`n" 3)Gruppe Hinzufügen "`n" 4)Gruppe Löschen "`n" 5) Benutzer in eine Gruppe hinzufügen "`n" 6) Benutzer aus einer Gruppe Entfernen'),
[string] $Benutzer = (Read-Host 'Benutzernamen eingeben'),
[string] $Gruppe = (Read-Host 'Gruppennamen eingeben'),
[string] $Password = (Read-Host 'Password eingeben'),
[string] $BenInGru = (Add-LocalGroupMember $Gruppe $Benutzer),
[string] $BenAusGru = (Remove-LocalGroupMember $Gruppe $Benutzer),
[string] $SuchG = (Get-LocalGroup -Filter {Name -eq $Gruppe}) ,
[string] $SuchB = (Get-LocalUser -Filter {Name -eq $Benutzer}),
[string] $HinzuG = (New-LocalGroup $Gruppe),
[string] $HinzuB = (New-LocalUser $Benutzer -Password $Password ),
[string] $EntfG = (Remove-LocalGroup $Gruppe),
[string] $EntfB = (Remove-LocalUser $Benutzer)
)
switch ($HoL) {
1 {write-host "ja"; $HinzuB; break; }
2 {write-host "ja"; $EntfB; break; }
3 {write-host "ja"; $Gruppe; $HinzuG; break; }
4 {write-host "ja"; $EntfG; break; }
5 {
if ($SuchG ) {}
else {$HinzuG};
if ($SuchB ) {}
else {$HinzuB};
$BenInGru; break;
}
6 {$BenAusGru; break; }
default {exit}
}
}
Much more readable.
A couple of other things:
- The only real parameters in your function are $Benutzer, $Gruppe and $Password. The rest should not be declared as parameters, they are just variables.
- Those parameters really should not depend on read-host because that means you cant use the function in a pipeline.
- The default is for parameters to be optional. You have to specify if they are mandatory.