Ryan Tice
Ryan Tice

Reputation: 731

Powershell copy-item, store new folder as variable

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

Upvotes: 2

Views: 2765

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174445

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))

Upvotes: 1

Related Questions