Prevent Powershell from truncating output - powershell

Trying to figure out which file errored out, but ...hamburg (3).jpg isn't very descriptive.
Considering I got dozens of these errors, I have essentially no way of knowing what went wrong.
Rename-Item : Cannot create a file when that file already exists.
At O:\projects\scripts\restrict_name.ps1:66 char:7
+ } | Rename-Item -NewName {
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\usr\Do...hamburg (3).jpg:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
Why does Powershell allocate so little space to paths even their own path portion (C:\Users\usr\Downloads) is truncated - let alone any additional user directories - and is there a way to circumvent this?

Related

Error when trying to change file name to date created?

I'm using PowerShell to change a bunch of video file names from "IMG-3523" etc to their respective dates and hour/minute/second created.
However, for some reason, it's giving me an error saying something along the lines of "that file name already exists, cannot create duplicate". However this seems impossible, as I couldn't have taken 2 videos at the exact same start time (hh:mm:ss).
Here's the code I used (from this tutorial):
Get-ChildItem | Rename-Item -NewName {$_.LastWriteTime.ToString("yyyy-MM-dd hh.mm.ss ddd") + ($_.Extension)}
Here's the error I get:
Rename-Item : Cannot create a file when that file already exists.
At line:1 char:17
... ChildItem | Rename-Item -NewName {$_.LastWriteTime.ToString("yyyy-MM- ...
CategoryInfo : WriteError: (C:\Users\IMG_5956.MOV:String) [Rename-Item], IOException
FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

Rename-item: Cannot rename because item at ... does not exist [duplicate]

This question already has an answer here:
batch renaming files using powershell
(1 answer)
Closed 2 years ago.
I am lost with simple rename-item. Need to change names of folders to "01", "02", "03"... Tried everything but at the end I get this "Item doesn't exist". Sorry for dumb question but I am looking for a solution all day.
PS C:\Users\admin>
$nr = 1
Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
Rename-Item : Cannot rename because item at 'ert' does not exist.
At line:3 char:23
+ Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Cannot rename because item at 'ukh' does not exist.
At line:3 char:23
+ Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Cannot rename because item at 'yph' does not exist.
At line:3 char:23
+ Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
user14915444's helpful answer has provided the crucial pointer:
The problem is that Windows PowerShell situationally stringifies Get-ChildItem (dir) output objects by file name rather than by full path, necessitating the use of $_.FullName rather than just $_; the problem has been fixed in PowerShell [Core] v6.1+ - see this answer for details.
However, in your case the problem can be avoided altogether, by piping the Get-ChildItem output directly to Rename-Item, using a delay-bind script block, which also speeds up the operation:
[ref] $nr = 1
Get-ChildItem E:Data-test | Rename-Item -NewName { '{0}' -f $nr.Value++ } -WhatIf
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.
Note the use of a [ref] variable, which enables incrementing the sequence number across input objects from inside the delay-bind script block. This is necessary, because delay-bind script blocks run in a child scope of the caller's scope - unlike the script blocks passed to ForEach-Object (%) and Where-Object (?).
See GitHub issue #7157 for a discussion of this discrepancy.
Your script works fine. (At least the first variation.) You can't run it but once because you rename the file it is working with (i.e. "Data-Test"). I just created a Data-Test file on my E: drive and ran your script without issue once. Then, on second attempt, it couldn't work because it worked the first time.
If you are working with the contents of a directory named "Data-Test," you need to append your $_ with .FullName. (EX: Rename-Item $_.FullName -NewName ...)
See image:
If it still doesn't work, ensure that you have a file/folder named "Data-Test" in the CWD of your E: drive. Placing a backslash in the location will guarantee you are working in the root. (EX: E:\Data-Test)

Powershell Script "$_.FullName" not working - Sending files via "Copy-VMFile"

I'm trying to use a script I found on the internet to copy all files in a directory but I can't get it to work. Can anybody help debug? I'm guessing the script was used to transfer windows/windows but I need windows --> Linux.
https://www.powershellmagazine.com/2013/12/17/pstip-copying-folders-using-copy-vmfile-cmdlet-in-windows-server-2012-r2-hyper-v/
Get-ChildItem C:\tmp -Recurse -File | % { Copy-VMFile -Name "OpenProject8.3" -SourcePath $_.FullName -DestinationPath "/tmp/" -FileSource Host }
The issue seems to be related to the sourcepath, but im not 100%.
Copy-VMFile : Failed to initiate copying files to the guest.
Failed to copy the source file 'C:\tmp\svn-repositories-20200212010002.tar.gz' to the destination '/tmp/' in the guest.
At line:1 char:43
+ ... -File | % { Copy-VMFile -Name "OpenProject8.3" -SourcePath $_.FullNam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-VMFile], VirtualizationException
+ FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.CopyVMFile
Well.... I feel stupid! The issue was that earlier testing the x-fer had succeeded and then when the file exists, Copy-VMFile won't overwrite the file (even though it uses root account...) and gives a non-descriptive error! The code above works fine as is.

Rename-Item is not working

I have a share on a folder at path \\Srv1011\User0$ (Active Directory) and I want to rename it with another name \\Srv1011\User1$ but it is not working for me.
Code is:
Rename-Item -Path \\Srv1011\User0$ -NewName \\Srv1011\User1$
Right now the share is enable on the folder and if I execute the above command I get this error:
Rename-Item : Object reference not set to an instance of an object.
At line:1 char:1
+ Rename-Item -Path \\srv13577\User0$ -NewName \\srv13577\User1$
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Rename-Item], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.RenameItemCommand
But If I remove the share from folder I get the following error:
Rename-Item : Cannot rename because item at '\\srv13577\User0$' does not exist.
At line:1 char:1
+ Rename-Item -Path \\srv13577\User0$ -NewName \\srv13577\User1$
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Right now I am not sure how to rename such folder. My original work is to drop the share, rename the folder and create the share back.
But for now I am stuck with renaming.
To explain your misconception:
What you are trying to achieve is renaming an smb share, (Look at your path, which starts with \\). Rename-Item is not able to rename shares this way.
You probably want to rename the folder, which is shared under User0$ on the server, or you want to chamge the share's name. These are two different actions.
When you unshare your folder and try to rename it again, it can't be found, because - yeah - the share under \\srv13577\User0$ is not there anymore. That's why your 2nd error came up.
There is no way to rename a share folder using rename-item cmdlet.
Rather I would suggest you to take the content of the share folder,
then create a new share folder with your new name
then move the content from the old one to new one
and finally delete the source share folder.
That will ease out your work.

Copy-Item with over write

In one of my script, it tries to copy an exe to C:\Windows\Temp folder. For example:
Copy-Item repo\filename.exe -Destination \\$machine_name\C$\Windows\Temp
Error Message:
Copy-Item : The network path was not found.
At D:\CMPortal\Scripts\ClientRepair\RepairCCMClient.ps1:122 char:33
+ if(Copy-Item <<<< cmsetup.exe -Destination \\$install_cmexec\C$\Windows\Temp)
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
When I run the same command manually, it works. Dont know whats wrong.
Is there any to overwrite the file if the copying file exists in the destination?
Thanks in advance.
Are you using the quotes correctly?
Copy-Item 'repo\filename.exe' -Destination "\\$machine_name\C`$\Windows\Temp" -Force
Does $install_cmexec contains a server name?
Also, make sure the user who runs the script has the appropriate permissions on the target path.