Help with an "if...else" statement in Powershell

Hello Experts,

I’m having the worst time with an if…else statement in Powershell.  Basically what I want to do is have Powershell check to see if a folder exists in a specified location.  If it doesn’t exist, I want it to:
1.       Create the folder
2.      Create a log file within the folder
3.      Add entries into the log file

I have this part working.  Where I’m getting messed up is in the “else” part.  Basically what I want to happen is if the folder already exists, then an entry into the log file is added.  My powershell code is as follows:
$ArchiveDate = Get-Date -Format yyyyMMdd
$Archivefile = "$ArchiveDate.txt"
$ArchiveFilePath = "\\mydomain.local\shares\users\nickd\target"




if(-not(test-path -Path "$ArchiveFilePath\$ArchiveDate.txt" ))
{
Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) A process has checked to see if the archive folder exists."
New-Item -Path "$ArchiveFilePath\$ArchiveDate" -ItemType directory
New-Item -Name "$ArchiveDate`_log.txt" -Path "$ArchiveFilePath\$ArchiveDate" -ItemType file
Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) The archive folder '$ArchiveDate`_log.txt' has been created"
Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) The log file '$ArchiveDate`_log.txt' has been created"
}
Else
{
       Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) A process has checked to see if the archive folder exists." 
}

Open in new window


Thanks in advance for your help.

Nick
LVL 1
ndalmolin_13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmcgOwnerCommented:
Do you need a three way logic path, for the case that the folder exists but not the file that you want?
footechCommented:
Mostly you need to get your order of events worked out.  It's pretty hard to write to the file if it doesn't exist, but Add-Content will actually create the file if it doesn't exist, IF the folder does exist.
$ArchiveDate = Get-Date -Format yyyyMMdd
$Archivefile = "$ArchiveDate.txt"
$ArchiveFilePath = "\\mydomain.local\shares\users\nickd\target"

If ( -not( Test-Path -Path "$ArchiveFilePath\$ArchiveDate.txt" ) )
{
    If ( -not( Test-Path -Path $ArchiveFilePath) )
    {
        New-Item -Path "$ArchiveFilePath\$ArchiveDate" -ItemType directory
        Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) The archive folder '$ArchiveDate`_log.txt' has been created (and now so has the file)"
    }
    Else
    {
        Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) The log file '$ArchiveDate`_log.txt' has been created"
    }
}
Else
{
    Add-Content -Path "$ArchiveFilePath\$ArchiveDate\$ArchiveDate`_log.txt" -Value " $(get-date -Format yyyyMMdd:hh:mm:ss) A process has checked to see if the archive folder exists." 
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ndalmolin_13Author Commented:
Thanks for the explaination
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.