Getting started with the PowerShell If Statement

preview_player
Показать описание
In this video, we do an introduction to using the PowerShell If statement and all of the nooks and crannies that go along with it. We look at If, ElseIf, and If and how you can get the most out of them.

Help article for comparison operators

Рекомендации по теме
Комментарии
Автор

These videos continues to be useful for me!

Corrado
Автор

Hi Shane, I'm really enjoying your powershell series. Thanks!

monkeybum
Автор

You are fantastic! Been having so much trouble with my IF ELSE statment and you have shown me a much simpler way to get a better result! thank you

oliverjeffries
Автор

Great Stuff! Thanks for breaking it down to easy-to-understand terms/steps for someone starting out in PowerShell.

gilgameshxg
Автор

Shane you are better than my teacher, thanks for your amazing job, I got subbed right now
Cheers from Barcelona <3

JesterV
Автор

are AWESOME!!!! thank you very much for sharing so interesting videos like these!

lahernandezs
Автор

Thanks Shane for another great video. I particularly like your error handling routine. Nice and simple. :)

scottdimond
Автор

Very informative. Good section on comparison operators!

hassanmalik
Автор

Thanks for the video. Much appreciated

SJ-rpkf
Автор

Hey man, thanks for these videos. They are really helping me learn PS.

AllenAndersonTaylor
Автор

Thanks, this helped me with me text based RPG game in PS!

GoingMod
Автор

Great videos! I have a question about alias is there a way to replace all the alias’ in code auto magically, or a way to convert?

GixxerDaveNeo
Автор

I'm having trouble is there an e-mail I could use to contact you to ask you a few questions about my script ?

picnicsinspace
Автор

I'm having trouble with validating the value when it belongs to the status of a service (opposite to a Where-Object command where no matter what, it's always valid - Argh).
In the below example, I would like it to output the word 'talk' to screen when the IF is valid.
As I show in my example below, the variable value currently is "stopped", but IF does not see it so.
Where am I going wrong here?



PS C:\Scripts> $Service

Status Name DisplayName
----
Stopped WebClient WebClient



PS C:\Scripts> If($Service -eq "Stopped") {'talk'}

PS C:\Scripts> If($Service -eq 'Stopped') {'talk'}

PS C:\Scripts> If($Service -eq 1) {'talk'}

PS C:\Scripts> If($Service -eq 4) {'talk'}


PS C:\Scripts> If($Service -eq Stopped) {'talk'}
At line:1 char:16
+ If($Service -eq Stopped) {'talk'}
+ ~
You must provide a value expression following the '-eq' operator.
At line:1 char:17
+ If($Service -eq Stopped) {'talk'}
+
Unexpected token 'Stopped' in expression or statement.
At line:1 char:17
+ If($Service -eq Stopped) {'talk'}
+
Missing closing ')' after expression in 'If' statement.
At line:1 char:24
+ If($Service -eq Stopped) {'talk'}
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [],
+ FullyQualifiedErrorId : ExpectedValueExpression


PS C:\Scripts>



Thanks for the help in advance.

Promos
Автор

Please help me with these questions I'm having a hard time figuring them out:


1-Write the PowerShell code necessary to prompt the user to enter a number and store that value in a variable called $userPIN using a datatype of Int32.
2-Check if the userPIN entered is positive and if not print out an error message.
3-Check if the userPIN entered is at least 4 digits long and if not print out an error message.

picnicsinspace
Автор

I need PowerShell tutor can you help me, we can work something out.

joevanynguyen
Автор

i have made this $names=@('Audiosrv', 'BFE', 'BITS', 'Dhcp', 'Dnscache', 'DNS')
and then made this foreach($item in $names){get-service $item|select displayname, name, starttype, status}
i dont have service named DNS so i need to implement if statement that it could print that nonexisting service into a custom named .csv file and all others that are found in the list i need to put those to in seperate .csv file.
if you will reply me fast i would be grateful dude.

zubena
Автор

sorry, that was weird. hahahaha i laughed so hard in the office

anthonyfreeman
Автор

I appreciate professionals making content. That's why I paid for YouTube Premium. I do wish your introduction was not so verbose.

justme-izec
Автор

I think you should better consider that people who are following powershell lessons, are expected to have a minimum experiance of at least one easy programming language. Powershell is not the kind of topic for starters in programming language to learn how an if statement works. Powershell is not good nor suitable to reach their needs.

I believe that such basic issues in programming syntax and further details of the if statements, arrays, variables etc basics, are destructing the purpose of the most of the followers of your lessons, becasue they miss some powershell semantics while they are jumping time-bar during each lesson.

I guess a short example for each of the basic statements, emphasising the most common differences, in a single lesson, would be enough to explain for the followers who can really benefit these lessons.

tathineos