Question

$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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top