PowerShell copy and delete questionI have a script that I downloaded on the net and needed to modify a couple of items. I have bolded the three lines in question and comented them below. <br/> Here is the script:<br/> <br/> ##<br/> ##    Create a backup of all the vm's<br/> ##<br/> <br/> $dest = &quot;D:\VMbackup&quot;<br/> $dest2 = &quot;D:\TapeBackup&quot;<br/> $Date = (Get-Date).toString('ddMMyyyy')<br/> <strong>$Now = (Get-Date -Format ddMMyyy).AddDays(-21)</strong> &lt;- powershell does not like this, but I not know how else to do it.<br/> $VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService<br/> $ListofVMs = get-wmiobject -namespace root\virtualization Msvm_ComputerSystem -filter  &quot;ElementName &lt;&gt; Name&quot;   <br/> <br/> foreach ($VM in [array] $ListOfVMs) <br/> {<br/>     $VMReturnState = $VM.EnabledState<br/>     $VMName = $VM.ElementName<br/> <br/>     if (($VM.EnabledState -eq 2) -or ($VM.EnabledState -eq 32768) -or ($VM.EnabledState -eq 32770))<br/>     {<br/>         $VM.RequestStateChange(32769)<br/>         echo &quot;Saving the state of $VMName&quot;<br/>     }<br/> <br/>     while (!($VM.EnabledState -eq 32769) -and !($VM.EnabledState -eq 3))<br/>     {<br/>         Start-Sleep(1)<br/>         $VM = get-wmiobject -namespace root\virtualization -Query &quot;Select * From Msvm_ComputerSystem Where ElementName='$VMName'&quot;<br/>     } <br/> <br/> <br/>     if ([IO.Directory]::Exists(&quot;$dest\TmpDir\$VMName&quot;))<br/>     {<br/>         [IO.Directory]::Delete(&quot;$dest\TmpDir\$VMName&quot;, $True)<br/>     }<br/> <br/>     echo &quot;Exporting the VM&quot;<br/>     $status = $VM_Service.ExportVirtualSystem($VM.__PATH, $True, &quot;$dest\TmpDir&quot;)<br/>         <br/>     if ($status.ReturnValue -eq 4096)<br/>     {<br/>         $job = [Wmi]$status.Job    <br/>     <br/>         while (!($job.PercentComplete -eq 100) -and ($job.ErrorCode -eq 0))<br/>         {<br/>             Start-Sleep(5)<br/>             $job = [Wmi]$status.Job    <br/>             echo $job.PercentComplete<br/>         } <br/>     }<br/> <br/> <br/>     ##    Store the files on in a temp directory before moving them to their location and then remove the old files.<br/> <br/>     if ([IO.Directory]::Exists(&quot;$dest\$VMName&quot;))<br/>     {<br/>         [IO.Directory]::Move(&quot;$dest\$VMName&quot;, &quot;$dest\$VMName-OldTmpDir&quot;)<br/>         <strong>[IO.Directory]::Copy(&quot;$dest\TmpDir\$VMName&quot;, &quot;$dest2\$VMName&quot;)</strong> &lt;- make a copy of this folder before move and put in another folder<br/>         [IO.Directory]::Move(&quot;$dest\TmpDir\$VMName&quot;, &quot;$dest\$VMName-$Date&quot;)<br/>         <strong>[IO.Directory]::Delete(&quot;$dest\$VMName-$Now&quot;, $True)</strong> &lt;- Delete the folder it the folder+date is equal to 21 days<br/>         [IO.Directory]::Delete(&quot;$dest\$VMName-OldTmpDir&quot;, $True)<br/>     }<br/>     else<br/>     {<br/>         [IO.Directory]::Move(&quot;$dest\TmpDir\$VMName&quot;, &quot;$dest\$VMName-$DateD&quot;)<br/>     }<br/>     <br/>     echo &quot;Done with $VMName&quot;<br/>     $VM.RequestStateChange($VMReturnState)<br/> }<br/>© 2009 Microsoft Corporation. All rights reserved.Fri, 11 Sep 2009 18:23:52 Zd289313f-ff82-4d2d-8791-51c732b6e530http://social.technet.microsoft.com/Forums/en/ITCG/thread/d289313f-ff82-4d2d-8791-51c732b6e530#d289313f-ff82-4d2d-8791-51c732b6e530http://social.technet.microsoft.com/Forums/en/ITCG/thread/d289313f-ff82-4d2d-8791-51c732b6e530#d289313f-ff82-4d2d-8791-51c732b6e530Nua-Ohttp://social.technet.microsoft.com/Profile/en-US/?user=Nua-OPowerShell copy and delete questionI have a script that I downloaded on the net and needed to modify a couple of items. I have bolded the three lines in question and comented them below. <br/> Here is the script:<br/> <br/> ##<br/> ##    Create a backup of all the vm's<br/> ##<br/> <br/> $dest = &quot;D:\VMbackup&quot;<br/> $dest2 = &quot;D:\TapeBackup&quot;<br/> $Date = (Get-Date).toString('ddMMyyyy')<br/> <strong>$Now = (Get-Date -Format ddMMyyy).AddDays(-21)</strong> &lt;- powershell does not like this, but I not know how else to do it.<br/> $VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService<br/> $ListofVMs = get-wmiobject -namespace root\virtualization Msvm_ComputerSystem -filter  &quot;ElementName &lt;&gt; Name&quot;   <br/> <br/> foreach ($VM in [array] $ListOfVMs) <br/> {<br/>     $VMReturnState = $VM.EnabledState<br/>     $VMName = $VM.ElementName<br/> <br/>     if (($VM.EnabledState -eq 2) -or ($VM.EnabledState -eq 32768) -or ($VM.EnabledState -eq 32770))<br/>     {<br/>         $VM.RequestStateChange(32769)<br/>         echo &quot;Saving the state of $VMName&quot;<br/>     }<br/> <br/>     while (!($VM.EnabledState -eq 32769) -and !($VM.EnabledState -eq 3))<br/>     {<br/>         Start-Sleep(1)<br/>         $VM = get-wmiobject -namespace root\virtualization -Query &quot;Select * From Msvm_ComputerSystem Where ElementName='$VMName'&quot;<br/>     } <br/> <br/> <br/>     if ([IO.Directory]::Exists(&quot;$dest\TmpDir\$VMName&quot;))<br/>     {<br/>         [IO.Directory]::Delete(&quot;$dest\TmpDir\$VMName&quot;, $True)<br/>     }<br/> <br/>     echo &quot;Exporting the VM&quot;<br/>     $status = $VM_Service.ExportVirtualSystem($VM.__PATH, $True, &quot;$dest\TmpDir&quot;)<br/>         <br/>     if ($status.ReturnValue -eq 4096)<br/>     {<br/>         $job = [Wmi]$status.Job    <br/>     <br/>         while (!($job.PercentComplete -eq 100) -and ($job.ErrorCode -eq 0))<br/>         {<br/>             Start-Sleep(5)<br/>             $job = [Wmi]$status.Job    <br/>             echo $job.PercentComplete<br/>         } <br/>     }<br/> <br/> <br/>     ##    Store the files on in a temp directory before moving them to their location and then remove the old files.<br/> <br/>     if ([IO.Directory]::Exists(&quot;$dest\$VMName&quot;))<br/>     {<br/>         [IO.Directory]::Move(&quot;$dest\$VMName&quot;, &quot;$dest\$VMName-OldTmpDir&quot;)<br/>         <strong>[IO.Directory]::Copy(&quot;$dest\TmpDir\$VMName&quot;, &quot;$dest2\$VMName&quot;)</strong> &lt;- make a copy of this folder before move and put in another folder<br/>         [IO.Directory]::Move(&quot;$dest\TmpDir\$VMName&quot;, &quot;$dest\$VMName-$Date&quot;)<br/>         <strong>[IO.Directory]::Delete(&quot;$dest\$VMName-$Now&quot;, $True)</strong> &lt;- Delete the folder it the folder+date is equal to 21 days<br/>         [IO.Directory]::Delete(&quot;$dest\$VMName-OldTmpDir&quot;, $True)<br/>     }<br/>     else<br/>     {<br/>         [IO.Directory]::Move(&quot;$dest\TmpDir\$VMName&quot;, &quot;$dest\$VMName-$DateD&quot;)<br/>     }<br/>     <br/>     echo &quot;Done with $VMName&quot;<br/>     $VM.RequestStateChange($VMReturnState)<br/> }<br/>Thu, 02 Jul 2009 20:41:25 Z2009-07-02T20:41:25Zhttp://social.technet.microsoft.com/Forums/en/ITCG/thread/d289313f-ff82-4d2d-8791-51c732b6e530#979f07f0-3b73-43e4-810f-51a8c8631007http://social.technet.microsoft.com/Forums/en/ITCG/thread/d289313f-ff82-4d2d-8791-51c732b6e530#979f07f0-3b73-43e4-810f-51a8c8631007Grégory Schirohttp://social.technet.microsoft.com/Profile/en-US/?user=Gr%u00e9gory%20SchiroPowerShell copy and delete questionHi,<br/> <br/> Regarding your first bolded line : <strong>$Now = (Get-Date -Format ddMMyyy).AddDays(-21)<br/> </strong> Here <strong>(Get-Date -Format ddMMyyy)</strong> returns a string not a DateTime object. So there is no method called AddDays on this object. You have to modify date using AddDays before formating it.<br/> <br/> By Example : <strong>(Get-Date).AddDays(-21).ToString('ddMMyyyy')</strong> <br/> <br/> <br/> Then, prefer to use built-in <strong>Copy-Item</strong> , <strong>Remove-Item</strong> or <strong>Move-Item</strong> Cmdlets for copying, removing or moving items.<br/> <br/> Hope it helps.<br/><hr class="sig">Grégory Schiro - PowerShell MVP - <a href="http://scriptingof.blogspot.com">PowerShell &amp; MOF</a>Fri, 03 Jul 2009 08:13:01 Z2009-07-03T08:13:01Zhttp://social.technet.microsoft.com/Forums/en/ITCG/thread/d289313f-ff82-4d2d-8791-51c732b6e530#84f4fa08-f2ad-4879-b365-207f2eeba9dehttp://social.technet.microsoft.com/Forums/en/ITCG/thread/d289313f-ff82-4d2d-8791-51c732b6e530#84f4fa08-f2ad-4879-b365-207f2eeba9deJakob Svendsenhttp://social.technet.microsoft.com/Profile/en-US/?user=Jakob%20SvendsenPowerShell copy and delete question<br/><br/>Hello<br/><br/>first one is easy to fix :)<br/><br/><strong>$Now = (Get-Date -Format ddMMyyy).AddDays(-21)</strong> <br/>should be<br/><strong>$Now = (Get-Date).AddDays(-21)</strong> <br/>Cause the format function , causes the cmdlet to output a system.string instead of system.datetime, therefore you cannot run the adddays method (since it only exists on system.datetime).<br/><br/>i am looking into the others.<br/><br/><strong>[IO.Directory]::Copy(&quot;$dest\TmpDir\$VMName&quot;, &quot;$dest2\$VMName&quot;)</strong> <br/><br/>I am not sure why u are using these methods directly?<br/>i would prefer to use the cmdlets provided by powershell.<br/>you can use this command instead of the abiove line<br/><br/><br/>Copy-Item -Recurse &quot;$dest\TmpDir\$VMName&quot; &quot;$dest2\$VMName&quot;<br/><br/><strong>[IO.Directory]::Delete(&quot;$dest\$VMName-$Now&quot;, $True)</strong> <br/><br/>I am not sure what errors you get, but this command will not delete the folder if it is not empty.<br/>i would again use the cmdlet instead like this:<br/><br/>Remove-Item -Recurse <strong>&quot;$dest\$VMName-$Now&quot;</strong><br/><br/><br/>Good luck<br/><br/><br/>Best Regards<br/>Jakob Gottlieb svendsen<hr class="sig">Trainer/Consultant - Coretech A/S - <a href="http://blog.coretech.dk/author/jgs">Blog</a> - MCT - MCTS - VB.NET - C#.NET - Powershell - VBScriptFri, 03 Jul 2009 08:20:48 Z2009-07-03T08:20:48Zhttp://social.technet.microsoft.com/Forums/en/ITCG/thread/d289313f-ff82-4d2d-8791-51c732b6e530#c9d152d7-9353-42ef-95c9-0017f9903cd1http://social.technet.microsoft.com/Forums/en/ITCG/thread/d289313f-ff82-4d2d-8791-51c732b6e530#c9d152d7-9353-42ef-95c9-0017f9903cd1Nua-Ohttp://social.technet.microsoft.com/Profile/en-US/?user=Nua-OPowerShell copy and delete question<p>I have made the requested changes and for the most part it is working except for the rename-item part I have bolded below. I get this error<br/>Rename-Item : Cannot rename because the target specified is not a path.<br/><br/>$dest1 = &quot;D:\VMbackup&quot;<br/>$dest2 = &quot;D:\TapeBackup&quot;<br/>$Date = (Get-Date).toString('ddMMyyyy')<br/>$Now = (Get-Date).AddDays(-21)<br/>$VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService<br/>$ListofVMs = get-wmiobject -namespace root\virtualization Msvm_ComputerSystem -filter  &quot;ElementName &lt;&gt; Name&quot;  </p> <p>foreach ($VM in [array] $ListOfVMs) <br/>{<br/> $VMReturnState = $VM.EnabledState<br/> $VMName = $VM.ElementName</p> <p> if (($VM.EnabledState -eq 2) -or ($VM.EnabledState -eq 32768) -or ($VM.EnabledState -eq 32770))<br/> {<br/>  $VM.RequestStateChange(32769)<br/>  echo &quot;Saving the state of $VMName&quot;<br/> }</p> <p> while (!($VM.EnabledState -eq 32769) -and !($VM.EnabledState -eq 3))<br/> {<br/>  Start-Sleep(1)<br/>  $VM = get-wmiobject -namespace root\virtualization -Query &quot;Select * From Msvm_ComputerSystem Where ElementName='$VMName'&quot;<br/> }</p> <p><br/> if ([IO.Directory]::Exists(&quot;$dest1\TmpDir\$VMName&quot;))<br/> {<br/>  [IO.Directory]::Delete(&quot;$dest1\TmpDir\$VMName&quot;, $True)<br/> }</p> <p> echo &quot;Exporting the VM&quot;<br/> $status = $VM_Service.ExportVirtualSystem($VM.__PATH, $True, &quot;$dest1\TmpDir&quot;)<br/>  <br/> if ($status.ReturnValue -eq 4096)<br/> {<br/>  $job = [Wmi]$status.Job <br/> <br/>  while (!($job.PercentComplete -eq 100) -and ($job.ErrorCode -eq 0))<br/>  {<br/>   Start-Sleep(5)<br/>   $job = [Wmi]$status.Job <br/>   echo $job.PercentComplete<br/>  } <br/> }</p> <p><br/> ## Store the files on in a temp directory before moving them to their location and then remove the old files.</p> <p> if ([IO.Directory]::Exists(&quot;$dest1\$VMName&quot;))<br/> {<br/>  Move-Item &quot;$dest1\$VMName&quot;, &quot;$dest1\$VMName-OldTmpDir&quot;<br/>  Copy-Item -recurse -force &quot;$dest1\TmpDir\$VMName&quot;, &quot;$dest2\$VMName&quot;<br/>  Move-Item &quot;$dest1\TmpDir\$VMName&quot;, &quot;$dest1\$VMName-$Date&quot;<br/>  Remove-Item -recurse &quot;$dest1\$VMName-$Now&quot;<br/>  Remove-Item -recurse &quot;$dest1\$VMName-OldTmpDir&quot;<br/> }<br/> else<br/> {<br/>  Move-Item $dest1\TmpDir\$VMName $dest1\$VMName<br/> <strong> Rename-Item -path &quot;$dest1\$VMName&quot; -newname &quot;$dest1\$VMName-$Now&quot;</strong><br/> }<br/> <br/> echo &quot;Done with $VMName&quot;<br/> $VM.RequestStateChange($VMReturnState)<br/>}<br/><br/>Thanks for the help...I am very close now!</p>Fri, 11 Sep 2009 18:23:52 Z2009-09-11T18:23:52Z