×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

IF statement codeblock (NEWBIE ALERT!)

IF statement codeblock (NEWBIE ALERT!)

IF statement codeblock (NEWBIE ALERT!)

(OP)
I am trying to get a Powershell script to run. It has an if block. When I keep the code block it executes on the same line like this...

If ($DirExists -eq $True) {New-Item ($DefaultPath + "\" + $DateCode) -Type Directory}

It executes fine. But I need to execute a few more lines so I tried this...

If ($DirExists -eq $True) {
New-Item ($DefaultPath + "\" + $DateCode) -Type Directory
}

and I get the prompt of....
>> New-Item ($DefaultPath + "\" + $DateCode) -Type Directory
>>}

I need to execute more than one line in the If statement.

Any ideas?

Thanks

RE: IF statement codeblock (NEWBIE ALERT!)

First of all, you don't have to validate if $DirExists -eq $true. Just look for $DirExists. Second, you can assemble the path a little easier.

CODE --> PowerShell

if ($DirExists){ New-Item "$DefaultPath\$DateCode" -Type Directory # do more stuff here }

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.

RE: IF statement codeblock (NEWBIE ALERT!)

(OP)
Here's what I get doing that...

PS C:\Users\Administrator> $DefaultPath = "C:\Temp"
PS C:\Users\Administrator> $DirExists = Test-Path $DefaultPath
PS C:\Users\Administrator>
PS C:\Users\Administrator> if ($DirExists){
>> New-Item "$DefaultPath\$DateCode" -Type Directory
>> # do more stuff here
>> }

Why do I get the ">>" prompt?
If I hit enter twice it then finishes the script.

RE: IF statement codeblock (NEWBIE ALERT!)

(OP)
Oh yeah, I'm on v2.0 if that matters...
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1

RE: IF statement codeblock (NEWBIE ALERT!)

You get the >> because you're inside of something. You see the opening brack at the end of the if statement? You get the >> until you close that (which is done on the last line). That's expected.

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close