Windows Vista Forums

PowerShell equivalent of If (not) exist
  1. #1


    greatbarrier86 Guest

    PowerShell equivalent of If (not) exist

    Hi,



    Is there an equivalent to this command? I can't seem to find one but there
    has to be one.

    Jason

      My System SpecsSystem Spec

  2. #2


    Brandon Shell [MVP] Guest

    Re: PowerShell equivalent of If (not) exist

    The if statement will do it, but I think what your looking for is Test-Path
    Get-Help Test-Path -full

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    g> Hi,
    g>
    g> Is there an equivalent to this command? I can't seem to find one but
    g> there has to be one.
    g>
    g> Jason
    g>



      My System SpecsSystem Spec

  3. #3


    Shay Levi Guest

    Re: PowerShell equivalent of If (not) exist


    You can use "!" or -not.

    Check the help for logical operators:
    PS > help about_logical_operator



    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com
    Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic



    > Hi,
    >
    > Is there an equivalent to this command? I can't seem to find one but
    > there has to be one.
    >
    > Jason
    >


      My System SpecsSystem Spec

  4. #4


    Brandon Shell [MVP] Guest

    Re: PowerShell equivalent of If (not) exist

    whoops... I totally forgot I could just link the TechNet site for Test-Path
    http://technet.microsoft.com/en-us/l.../bb978701.aspx

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    g> Hi,
    g>
    g> Is there an equivalent to this command? I can't seem to find one but
    g> there has to be one.
    g>
    g> Jason
    g>



      My System SpecsSystem Spec

  5. #5


    Jacob Saaby Nielsen Guest

    Re: PowerShell equivalent of If (not) exist

    Hey greatbarrier86,

    the exclamation mark is the "not" operator. E.g. "if (!$_.Exist)"

    Best Regards,
    Jacob Saaby Nielsen

    http://www.pipforhelvede.net
    gmail: jacob DOT saaby
    hotmail (IM): same as gmail

    > Hi,
    >
    > Is there an equivalent to this command? I can't seem to find one but
    > there has to be one.
    >
    > Jason
    >


      My System SpecsSystem Spec

  6. #6


    greatbarrier86 Guest

    Re: PowerShell equivalent of If (not) exist

    Thanks to all of you!

    btw, i like the Test-Path the best. what would be the best way to write that

    $Result = test-path -path "C:\Documents and Settings\NicoleH"
    if ($Result -eq False)
    {
    }

    or something else? That seems to complex

    "Jacob Saaby Nielsen" wrote:

    > Hey greatbarrier86,
    >
    > the exclamation mark is the "not" operator. E.g. "if (!$_.Exist)"
    >
    > Best Regards,
    > Jacob Saaby Nielsen
    >
    > http://www.pipforhelvede.net
    > gmail: jacob DOT saaby
    > hotmail (IM): same as gmail
    >

    > > Hi,
    > >
    > > Is there an equivalent to this command? I can't seem to find one but
    > > there has to be one.
    > >
    > > Jason
    > >
    >
    >
    >

      My System SpecsSystem Spec

  7. #7


    Brandon Shell [MVP] Guest

    Re: PowerShell equivalent of If (not) exist

    if(test-path -path "C:\Documents and Settings\NicoleH"){}
    or NOT
    if(!(test-path -path "C:\Documents and Settings\NicoleH")){}

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    g> Thanks to all of you!
    g>
    g> btw, i like the Test-Path the best. what would be the best way to
    g> write that
    g>
    g> $Result = test-path -path "C:\Documents and Settings\NicoleH"
    g> if ($Result -eq False)
    g> {
    g> }
    g> or something else? That seems to complex
    g>
    g> "Jacob Saaby Nielsen" wrote:
    g>

    >> Hey greatbarrier86,
    >>
    >> the exclamation mark is the "not" operator. E.g. "if (!$_.Exist)"
    >>
    >> Best Regards,
    >> Jacob Saaby Nielsen
    >> http://www.pipforhelvede.net
    >> gmail: jacob DOT saaby
    >> hotmail (IM): same as gmail

    >>> Hi,
    >>>
    >>> Is there an equivalent to this command? I can't seem to find one but
    >>> there has to be one.
    >>>
    >>> Jason
    >>>


      My System SpecsSystem Spec

  8. #8


    Brandon Shell [MVP] Guest

    Re: PowerShell equivalent of If (not) exist

    btw... when your using if() blocks it ALWAYS evaulates to $true or $false...
    if $true then {} happens. if $false the Else{} happens. You dont need -eq

    so
    if($result){"it was true"}else{"it was false"}

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    g> Thanks to all of you!
    g>
    g> btw, i like the Test-Path the best. what would be the best way to
    g> write that
    g>
    g> $Result = test-path -path "C:\Documents and Settings\NicoleH"
    g> if ($Result -eq False)
    g> {
    g> }
    g> or something else? That seems to complex
    g>
    g> "Jacob Saaby Nielsen" wrote:
    g>

    >> Hey greatbarrier86,
    >>
    >> the exclamation mark is the "not" operator. E.g. "if (!$_.Exist)"
    >>
    >> Best Regards,
    >> Jacob Saaby Nielsen
    >> http://www.pipforhelvede.net
    >> gmail: jacob DOT saaby
    >> hotmail (IM): same as gmail

    >>> Hi,
    >>>
    >>> Is there an equivalent to this command? I can't seem to find one but
    >>> there has to be one.
    >>>
    >>> Jason
    >>>


      My System SpecsSystem Spec

  9. #9


    greatbarrier86 Guest

    Re: PowerShell equivalent of If (not) exist

    Brandon,

    that is slightly confusing. Can you show me an example of what you mean?

    "Brandon Shell [MVP]" wrote:

    > btw... when your using if() blocks it ALWAYS evaulates to $true or $false...
    > if $true then {} happens. if $false the Else{} happens. You dont need -eq
    >
    > so
    > if($result){"it was true"}else{"it was false"}
    >
    > Brandon Shell
    > ---------------
    > Blog: http://www.bsonposh.com/
    > PSH Scripts Project: www.codeplex.com/psobject
    >
    > g> Thanks to all of you!
    > g>
    > g> btw, i like the Test-Path the best. what would be the best way to
    > g> write that
    > g>
    > g> $Result = test-path -path "C:\Documents and Settings\NicoleH"
    > g> if ($Result -eq False)
    > g> {
    > g> }
    > g> or something else? That seems to complex
    > g>
    > g> "Jacob Saaby Nielsen" wrote:
    > g>

    > >> Hey greatbarrier86,
    > >>
    > >> the exclamation mark is the "not" operator. E.g. "if (!$_.Exist)"
    > >>
    > >> Best Regards,
    > >> Jacob Saaby Nielsen
    > >> http://www.pipforhelvede.net
    > >> gmail: jacob DOT saaby
    > >> hotmail (IM): same as gmail
    > >>> Hi,
    > >>>
    > >>> Is there an equivalent to this command? I can't seem to find one but
    > >>> there has to be one.
    > >>>
    > >>> Jason
    > >>>
    >
    >
    >

      My System SpecsSystem Spec

  10. #10


    Roman Kuzmin Guest

    Re: PowerShell equivalent of If (not) exist

    I guess he means you can write

    if ($Result)

    instead of

    if ($Result -eq False)

    Besides, you should use special variables $true and $false instead of True
    and False.

    Thanks,
    Roman


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
PowerShell equivalent of If (not) exist problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
PowerShell Remoting equivalent of SSH scp Dave Clarke PowerShell 2 19 Feb 2009
powershell equivalent Max2006 PowerShell 1 26 Jun 2008
VBCRLF equivalent in Powershell? Victag PowerShell 2 15 May 2008
PowerShell equivalent for ECHO ON dtward@gmail.com PowerShell 3 08 Jan 2007
PowerShell equivalent of StartName Andrew Watt [MVP] PowerShell 1 28 Jun 2006