Tuesday, March 31, 2015

Powershell Move-Item does not create a new folder

While working on powershell I found out that while moving a folder from one location to another, powershell Move-Item command does not create top level folder if the destination folder does not contain any items already.

e.g. if folder A contains only one folder B which has a file MyFile.txt and we want to move folder B inside an empty folder C,

Get-ChildItem "C:\A" |% {Move-Item -Path $_.FullName -Destination "C:\C" -Force}

Above command does not create folder B at the destination location as expected, instead it only moves file the MyFile.txt directly under folder C. A workaround for this issue is just display full name of the folder using $_.FullName like explained below

Get-ChildItem "C:\A" |% {Write-Host $_.Name;Move-Item -Path $_.FullName -Destination "C:\C" -Force}

This command creates a folder B under folder C and moves file MyFile.txt under it.

No comments:

Secure micro services using jwt and ocelot

  Secure Microservices Using JWT With Ocelot in .NET Core (code-maze.com)