Remove-Item in powershell - powershell

The file still exists (because I can open it through powershell) but Remove-Item isn't able to find the location apparently. Do you have any idea why?

Looks like like you hit a similar problem I recently ran into. In my case I had a space at the end of a folder name, and Windows absolutely refused to delete it.
Turns out Windows has an alternate method for addressing files and folders where you have to prefix the path with \\?\.
In your case, probably need to try this:
Remove-Item "\\?\$Profile"
In my case I was working in the command prompt at the time and RD /Q /S "\\?\FilePath" worked. So if worst comes to worst, you might do a Write-Host $Profile, select and copy the resulting path, switch to the command prompt and try the RD /Q /S command.
In your case, what is causing the problem is almost certainly the special characters after the OneDrive\ in the path.

Related

ICACLS and unicode file/folder names - the tool changes characters and is unable to locate files/folders

I'm trying to fix permissions on users folders created with Folder Redirect. I need "Domain Admins" group to have full access. Everything works as long as folder name is not using unicode characters. And there a quite many folders like that :(
For example: ICACLS changes Ł to A, ł to B...
D:\Path\Path>icacls ęóąśłżźćń
icacls ↓ˇ♣[B|zD
↓ˇ♣[B: The filename, directory name, or volume label syntax is incorrect.
'zD' is not recognized as an internal or external command,
operable program or batch file.
D:\Path\Path>icacls ĘÓĄŚŁŻŹĆŃ
icacls ↑Ë♦ZA{y♠C
↑Ë♦ZA{y♠C: Successfully processed 0 files; Failed processing 1 files
The filename, directory name, or volume label syntax is incorrect.
I tried changing chcp to various values, nothing worked (by default my cmd uses 852). Tried changing fonts used by cmd, also didn't help.
Is it possible to make ICACLS understand Polish? :)
EDIT: weird thing is, when i use ICACLS as 'system', it behaves like above. When used with mu user rights, it properly reads unicode characters.
EDIT2:
where icacls ran as user
C:\Users\Administrator.CEO>where icacls
C:\Windows\System32\icacls.exe
where icacls ran as system
C:\Windows\system32>where icacls
C:\Windows\System32\icacls.exe
i try to run icacls commands from cmd window, not as .bat or .cmd files, for example
icacls "D:\FolderRedir\IT\Stanisław Smólski" /grant "Domain Admins":F
output changes ł and ó to other characters, and icacls is unable to locate the file/directory
Not sure what exactly was wrong - we had a problem with one ups today and server was forcefully restarted. After the restart (and unexpected Windows update that used the restart oportunity) i tried icacls again, and... it worked with all the Polish characters.
I guess the problem solved itself.

Cannot find file path error for dir /b - Copying all file names script

I am trying to use a script that I've used before in Powershell, that is supposed to copy and paste all the file names inside a folder, into a text file.
I was able to use this script on a different computer last week, but can't do it now on my laptop.
Is there something I am not aware of?
The error it's giving is
Cannot find path 'C:\b' because it does not exist.
I tried removing the /b and I got a list of all the files but with other extra data like mode, last time write, length, name, and the extension of each file.
I really need the plain file name only. How can I do this? Thanks in advance!
the /b flag only works in cmd, not PowerShell
So you could try
cmd
dir /b
exit
Which will open CMD in your terminal and execute dir /b as a normal cmd command rather than PowerShell
Alternatively, just use PowerShell's Get-ChildItem (or gci for short)
To get just the plain file names using Get-ChildItem, you can do something like this:
# Assign the folder items to a variable $x
$x = gci
# Get only the names of those items
$x.name
See SS64 for details on CMD's dir and PowerShell's Get-ChildItem

How do i use Get-clipboard output in a powershell script?

i'm trying to use text that is inside the clipboard inside a powershell script. So what the purpose for the script, i want to be able to copy a file directory and then run the script so it uses the copied directory as a automatic destination. So my idea was to do something like this:
Copy-Item C:\Users\gif.gif -Destination "Copied Directory"
I'm very new to powershell scripting, so an explenation of what is going on would be nice but not needed. I origionlly thought that this could work but nope XD, this should have been a simple project but yeah it wasn't meant to be easy.
Copy-Item C:\Users\J.J\Documents\TouchPortal/Make_30fps_gif.bat -Destination | Get-Clipboard
Would love to get some help with this, and thank you in advance!
To complement Steven's helpful answer:
In Windows PowerShell only, you can use Get-Clipboard's -Format parameter to request clipboard data other than text.
In PowerShell [Core, v6+], this is no longer supported, and text is indeed the only data type supported by Get-Clipboard.
In Windows PowerShell, if you've used File Explorer to copy a directory to the clipboard (using the regular Copy shortcut-menu command or the Ctrl+C keyboard shortcut), you can access it as System.IO.DirectoryInfo instance by passing -Format FileDropList to Get-Clipboard.
Note: -Format FileDropList returns a collection of file-system-info objects, so as to also support multiple files/directories having been copied to the clipboard; thanks to member-access enumeration, however, you can treat this collection like a single object - assuming truly only one file or directory was copied.
# Note: Windows PowerShell only.
# The .FullName property returns the full directory path.
Copy-Item C:\Users\gif.gif -Destination (Get-Clipboard -Format FileDropList).FullName
In PowerShell [Core, v6+] you'll indeed have to use the Shift+right-click method and select Copy as path from the shortcut menu, in order to ensure that a file/directory is copied as a (double-quoted, full) path string, as shown in Steven's answer.
In PowerShell core including versions 6 & 7 Get-Clipboard only works with text. If you use it after copying a folder it will return null.
In PowerShell 5.1 (Windows PowerShell) you can use the -Format parameter with Get-Clipboard
See mklement0's answer for a better description and example using -Format.
If you need to use the newer versions, you can use the shift + context menu choice > Copy as Path to get the folder's string path on to the clipboard, but that will quote the path. The quoted path will then be rejected by Copy-Item.
However, you could quickly replace the quotes like below.
Copy-Item 'C:\temp\BaseFile.txt' -Destination (Get-Clipboard).Replace('"',"")
Caution though, this seems hazardous and I wouldn't advise it. I use Get-Clipboard all the time to get data into a console session and can attest that it's too easy to make mistakes. The clipboard is so transient and it's use so ubiquitous that even if you make this work it's bound to burn you at some point.
Maybe you can elaborate on what you're trying to do and why. Then we can brainstorm the best approach.

Using xcopy instead of robocopy for wildcarded files

I'm working on an old 2003 server and I want to copy a folder for specific files while also retaining the folder structure.
Note: I am not allowed to install or add robocopy onto this server.
The command that I use on another server is usually something like this.
robocopy Application Application_small *.txt /E
This, of course, gives me a new directory with nothing but the .txt files and the same folder structure.
But of course, robocopy isn't on Server 2003. However, xcopy does exist. Is there an equivalent switch that I can use to get the same kind of result as robocopy?
Similar question was found here.
xcopy Application\*.txt Application_small /s

Dealing with blank folder names while using get-childitem in powershell

I built a script, that searches through all directories recursively with Get-ChildItem. The problem is, there exist directories with blank names (done with alt+255).
When the script encounters such a directory, it still lists the files in this directory, but does not search in its sub-directories.
I don't think it is possible in powershell. but you can skip to cmd and use
cmd -c dir $Location /s
that works!
As #Bert Levrau mentioned above you can do a recursive search in CMD. Using Get-ChildItem in Powershell with a folder that has an ALT + 255 name will throw it into an infinite recursive loop. You can invoke a CMD process from Powershell though using the following example: $result = cmd /c $directoryPath /s
At that point, you can work through the result to find the information that you need.