2

I am trying to create a simple script to move a directory to a secured location on our servers. I want to pull the new file location as a variable to include in an email that I have labeled $secure. Any help would be appreciated - Thanks!

Write-Host "Enter Package Location: " -NoNewLine -ForegroundColor Green
$package = Read-Host
Copy-Item -Path $package -Destination "C:\PS" -Force -Recurse
$secure = ???

1 Answer 1

1

Use the -PassThru parameter with Copy-Item:

$secure = Copy-Item -Path $package -Destination "C:\PS" -Force -Recurse -PassThru

to get the copied item(s)


To just get the new root directory (ie. "C:\PS\packagedir"), use Split-Path -Leaf to grab the folder name from $package and combine with the destination path using Join-Path:

Write-Host "Enter Package Location: " -NoNewLine -ForegroundColor Green
$package = Read-Host
$destination = "C:\PS"
Copy-Item -Path $package -Destination $destination -Force -Recurse
$secure = Get-Item (Join-Path $destination -ChildPath (Split-Path $package -Leaf))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Ideally I would like just the folder structure as the variable, not the contents in these directories though, is there a way to get only that?
@RyanTice you bet! Updated the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.