Powershell cmdlet: move file to Team Drive - powershell

I'm having a hard time moving a file from a local directory and into Team Drive.
I have a feeling I may be forced to step away from PS and find another route, which I really don't want to but here goes.
This command does not work:
Move-Item -Path 'C:\Program Files (x86)\FieldSmart View\Logs\BgSync.log' -Destination 'G:\Team Drives\LGE Prints\Logs\$env:computername.txt'
This command does work:
Move-Item -Path 'C:\Program Files (x86)\FieldSmart View\Logs\BgSync.log' -Destination C:\Users\ITAdmin\Desktop\Test\$env:computername.txt
The only difference is the destination.
When trying to move a file into Team Drive this is the error that is returned:
Move-Item : The given path's format is not supported.
At line:1 char:1
Move-Item -Path 'C:\Program Files (x86)\FieldSmart View\Logs\BgSync.l ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Move-Item], NotSupportedException
FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.MoveItemCommand
What can I do?

My guess is that you are using Single Quotes not Double Quotes when you want a variable replacement in your string. Your 2nd example has no quotes and the command line will turn that "text" into a string with replacement.
Run this:
"single"
'G:\Team Drives\LGE Prints\Logs\$env:computername.txt'
"double"
"G:\Team Drives\LGE Prints\Logs\$env:computername.txt"
This link looks good, or any other search for variable replacement.
https://kevinmarquette.github.io/2017-01-13-powershell-variable-substitution-in-strings/

Related

I am unable to change the extension of a multiple files using the powershell

To change the extension of files located at: C:\Users\mohit singh\Desktop\spotlight, I typed the following command in the PowerShell
C:\Users\mohit singh\Desktop\spotlight> ren *.* *.jpg
But I get the following error:
ren : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path"
argument and run the operation again.
At line:1 char:1
+ ren *.* *.jpg
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand
You're trying to use the ren command from within PowerShell. However, ren in Powershell is an alias to Rename-Item which is a different command. Powershell and cmd are different shells that use different command interpreters.
Your simplest option is just to run your command from a cmd window, instead of Powershell.
But if you wanted to use Powershell, you can do this by getting every file in the current folder, piping it to Rename-Item, then using the ChangeExtension .Net API to change its extension (which is safer than simple string replacement).
Get-ChildItem -File | Rename-Item -NewName { [io.path]::ChangeExtension($_.Name, "jpg") }
If you want to act on subfolders too, add -Recurse to Get-ChildItem.

Powershell Remove-Item Cmdlet error

I'm using powershell to remove a directory with the command:
Remove-Item $pathAsString -Recurse -Force
However, it gives me the following error:
Remove-Item : Cannot remove the item at 'C:\Path' because it is in use.
At line:1 char:1
+ Remove-Item "C:\Path" -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Remove-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RemoveItemCommand
I thought that was odd, because the directory shouldn't be in use. I can go into file explorer and delete the directory manually. I'm not really sure why this behavior is occurring. I'm fairly new to powershell, so don't understand exactly how it behaves.
My scripts interaction with the program includes:
Creating the folder
Downloading an MSI to the folder
Setting a variable to represent the MSI stored in the folder. Like so:
$MSIVariable = Get-ChildItem $Path | Where {&_.Name -eq "MSIName.msi"}
I'm assuming that something to do with the folder is within a stream of some sort, but don't know how I'd fix this issue.
EDIT:
Here is the code I use involving the folder:
Creating the Folder:
if(!(Test-Path $MSILocation))
{
New-Item $MSILocation -ItemType directory
}
Downloading the MSI:
$webClient = (New-Object System.Net.WebClient)
$downloadLocation = Join-Path $MSILocation $MSIName
$webClient.DownloadFile($downloadURL, $downloadLocation)

Copy-Item PowerShell silently continues when source is a network share that does not exist

I'm seeing a strange behavior with the "Copy-Item" cmdlet. It looks like the cmdlet errors out if the source location is a local location that does not exist, but completely ignores that if the source is a network location that does not exist.
For e.g.
Copy-Item \\server\share\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop
This command doesn't show any errors even if the source share is invalid. However,
Copy-Item c:\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop
throws an error that looks like:
Copy-Item : Cannot find path 'C:\thisdoesnotexist' because it does not exist.
At line:1 char:1
+ Copy-Item c:\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\thisdoesnotexist:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
What's going on here? I checked TechNet but couldn't see anything there that talked about this. My question is:
Is this expected?
Is there a way to make the cmdlet fail when source network share is invalid as well?
Thanks for any pointers!

How to pipe a directory name into Remove-Item?

I'm not very savvy with powershell, so any help is appreciated.
I have a drive that holds users profiles. I need to remove a specific file from each user's profile. It's structured something like this. (Names changed to protect the innocent.)
E:\Profiles
UserID
Documents
OtherFolders
DirectoryToDeleteFrom
FileToDelete.txt
UserID2
...
...
I could use the following command to delete all of the files I need to get to, but it's unbearably slow as it's recursing through all of the other folders under UserID.
#this one would work perfectly, but is terribly slow
Get-ChildItem -Path E:\Profiles -Include FileToDelete.txt -Recurse
I know exactly where each file resides, so I thought that I could do this.
(Get-ChildItem -Path 'E:\Profiles' -Exclude *.lnk)|ForEach-Object{Remove-item -path 'E:\Profiles\' + $_.Name + '\Path\To\File\FileToDelete.txt'}
But I get the following error message:
Remove-Item : A positional parameter cannot be found that accepts
argument '+'. At line:1 char:70
+ (Get-ChildItem -Path 'E:\Profiles' -Exclude *.lnk)|ForEach-Object{Remove-item ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
I thought it was because I was passing Remove-Item a bad file path, but the following command generates the correct paths.
(Get-ChildItem -Path 'E:\Profiles' -Exclude *.lnk)|ForEach-Object{'E:\Profiles\' + $_.Name + '\Path\To\File\FileToDelete.txt'}
So what am I doing wrong here?
If you construct a string on the fly to be used inline, by another command, then put brackets around the path:
..{Remove-item -path ('E:\Profiles\' + $_.Name + '\Path\To\File\FileToDelete.txt')}

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.