Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF statement codeblock (NEWBIE ALERT!)

Status
Not open for further replies.

0LaoyanG0

MIS
Nov 12, 2002
25
US
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
 
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:
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.
 
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.
 
Oh yeah, I'm on v2.0 if that matters...
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top