In Powershell, how do I delete a file with a hyphen (dash) in the filename? - powershell

In Powershell, when I try to delete a file with a hyphen in the nam like this:
remove-item 'C:\S3\op_netadmin-47.bak'
I get this error:
remove-item : Cannot remove item C:\S3\op_netadmin-47.bak: Access to the path 'C:\S3\op_netadmin-47.bak' is denied.
At line:1 char:1
+ remove-item 'C:\S3\op_netadmin-47.bak'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\S3\op_netadmin-47.bak:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
But if I rename the file to op_netadmin_47.bak, it works fine.
I've tried with and without double quotes as well as single quotes

I tried your file name with - and _, both work ok. I also tried to use " around file name which also works.
As #mklement0 mentioned in comments read-only files require -Force parameter
Make sure user running script has read/write access to given folder

Related

Not able to unlock locked windows file folder

I have one utility provided by Dev, Which takes the firmware package as input and decrypts its content. After decrypts, it asks for a physical device to flash this decrypted content into that device. But before flashing, it creates one locked folder on my machine.
I am trying to unlock that locked window file folder.
Nothing happened when I tried to open it by double-clicking on the folder icon. Also, Write error occurred When I wanted to rename it using PowerShell.
PS C:\Users\RajNegi\AppData\Roaming> rename-item
cmdlet Rename-Item at command pipeline position 1
Supply values for the following parameters:
Path: C:\Users\abc\AppData\Roaming\vsc.{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0}
NewName: C:\Users\abc\AppData\Roaming\vsc
rename-item : Access to the path 'C:\Users\abc\AppData\Roaming\vsc.{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0}' is denied.
At line:1 char:1
+ rename-item
+ ~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\abc...f-00c04f60b9f0}:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
Note: I already set Full control in folder permissions.

Use variable for URL in powershell BITS transfer

I have the following in a powershell script:
$src_url = "http://my_server/my_file.zip"
if (!(Test-Path $src_zip))
{
"Downloading $src_url"
Start-BitsTransfer -Source "$src_url" -Destination .\$src_zip
}
The output of which is
Downloading http://my_server/my_file.zip
Start-BitsTransfer : The server name or address could not be resolved
At C:\foo.ps1:18 char:5
+ Start-BitsTransfer -Source "$src_url" -Destination .\$src_zip
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-BitsTransfer], Exception
+ FullyQualifiedErrorId : StartBitsTransferCOMException,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferComma
nd
I have tried with, without double and single quotes around the URL. However if I type the URL out directly the name is resolved correctly. What should I do to expand $src_url?
Considering it works with hard-coding URL, the source variable should expand correctly in your syntax.
And from error you pasted, it may due to destination path resolution. Try to use full path in it. Example,
$src_zip = "C:\temp\abc.zip"
Please see if the destination file needs to be removed or you can use force parameter.

How to create a group with special characters using Powershell

I have been tasked with creating a number of active directory groups using PowerShell. The problem is all the group names contain double colons "::" (the naming standard was created long before I joined!).
When running the following command:
NEW-ADGroup –name "XX123::Test_Group_Creation" –groupscope Global –path "OU=TestOU,DC=TestDomain,DC=local"
The following error is received:
NEW-ADGroup : The name provided is not a properly formed account name
At line:1 char:1
+ NEW-ADGroup –name "XX123::Test_Group_Creation" –groupscope Global –path "OU=Test ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (CN=XX123::Test_...Domain,DC=local:String) [New-ADGroup], ADException
+ FullyQualifiedErrorId : The server is unwilling to process the request,Microsoft.ActiveDirectory.Management.Commands.NewADGroup
If you create the group manually (in the gui) you get the following warning but you are still able to create it:
How can I get around this issue. F.Y.I. I have tried to escape the colons with a back tick and also a forward slash, none of which have worked
The : character is not valid for the sAMAccountName attribute, so you need to replace : with another character (the GUI uses _) when creating the group. You can use the -replace operator to do this pretty easily in PowerShell.

extracting zip files with powershell

I am trying to extract a zip file to then be downloaded by remote people. Once extracted I will have a script that installs the package but I need to use a compressed folder and the remote users don't have 7zip or anything like it.
I have this but I keep getting an error -
$shell = new-object -com shell.application
$zip = $shell.NameSpace(“C:\name.zip”)
foreach($item in $zip.items())
{
$shell.Namespace(“C:\temp\name”).copyhere($item)
}
The error I am getting is below -
You cannot call a method on a null-valued expression.
At line:5 char:43
+ $shell.Namespace(“C:\name”).copyhere <<<< ($item)
+ CategoryInfo : InvalidOperation: (copyhere:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
There are other issues with the code but this is a work in progress, I will fix the hard coding of values once I can get it to extract.
Does your extract path exist (“C:\temp\name”)? To be clear, both of the items in that path should be directories.
That should be a path that already exists. If it doesn't you get that error.

Having issue removing a file in powershell

I'm trying to remove a file after checking some thing in that. I'm capturing the whole path into a variable and passing it to Remove-Item command and I'm getting below error:
Remove-Item : An object at the specified path E:\Import\IRIS_Import\working\RP
LS_BAI_20120719092600450-20120719093206.csv does not exist.
At E:\Import\IRIS_Import\FIX_IRIS_49_FILES_kr.ps1:53 char:13
+ Remove-Item <<<< $file
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], PSArgumentEx
ception
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RemoveIte
mCommand
Not sure why Powershell is complaining about file not being there even I can see the file there physically. Any ideas please?
I faced this exact same issue when I ran an Azure powershell webjob. I followed the answer in another thread in addition's to justinf's answer in this thread and modified my remove-item command as below.
remove-item filesystem::\\uncpath\folder
Your problem is that the file had an - in the file name and powershell does not like this it would seem.
This code worked for me. If it does not work for you post more of your code up and I will take a look.
$test = "c:\test\LS_BAI_20120719092600450-20120719093206.csv"
Remove-Item -LiteralPath $test