0
$backup = “c:\users\public\document\backup
$file = “$Home\documents\Win213Notes.txt
If (Test-Path $file) {
    mv $file $backup 
}
Else {
    Write-Host “File Not Found”
}

Is it smart to put pathnames into a variable?

chopper
  • 6,649
  • 7
  • 36
  • 53
Musa
  • 553
  • 1
  • 7
  • 19
  • 1
    I almost always create variables for Paths,it just helps my thinking! Incidentally, did something happen to your closing brackets? – Guy Thomas Feb 20 '14 at 13:34

1 Answers1

3

It depends. Do you use the path more than once? Will you re-use the script or function in a context where you might want to change the path? If you answered yes to either of those, then yes, you should probably use a variable for the path.

However, if you're using the console interactively, and you're going to use the path once, there's not much point in assigning it to a variable first. There's nothing wrong with it, but it doesn't really serve any purpose.

KevinD
  • 3,023
  • 2
  • 22
  • 26