locked
Conditionally add -WhatIf RRS feed

  • Question

  • Hi,

    Is it possible to conditionally add -WhatIf

    $logonly = $true
    Copy-Item -Path $dir.FullName -Destination $destdir -Recurse -Force if($logonly -eq $true) {-WhatIf}

    Instead of basically duplicating the entire line with only -WhatIf at the end:

    $logonly = $true
    if($logonly -eq $true)
    {
        Copy-Item -Path $dir.FullName -Destination $destdir -Recurse -Force -WhatIf
    }
    else
    {
        Copy-Item -Path $dir.FullName -Destination $destdir -Recurse -Force
    }

    Thanks

    Wednesday, December 18, 2019 9:50 PM

Answers

  • Copy-Item -Path$dir.FullName -Destination$destdir -Recurse -Force -WhatIf:$logononly



    \_(ツ)_/

    • Marked as answer by Lanky Doodle Wednesday, December 18, 2019 10:01 PM
    Wednesday, December 18, 2019 9:59 PM

All replies

  • Copy-Item -Path$dir.FullName -Destination$destdir -Recurse -Force -WhatIf:$logononly



    \_(ツ)_/

    • Marked as answer by Lanky Doodle Wednesday, December 18, 2019 10:01 PM
    Wednesday, December 18, 2019 9:59 PM
  • Cool, thanks! I did wonder if you could pass something to parameters.

    My way does actually work too!

    Wednesday, December 18, 2019 10:01 PM