Unable to delete a folder on Windows with a .git in it - windows

If I start a PowerShell as a regular user (not admin) and run
cd Desktop
mkdir test
cd test
git init
cd ..
I then cant delete the folder via PowerShell. I have tried the following commands as admin and as the current user.
rm -r test # -> no access permission
rmdir test # -> no access permission
del -r test # -> no access permission
del test # -> no access permission
It prints
+ CategoryInfo : PermissionDenied: (.git:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
But I don't understand why I can't remove folders with a .git folder in them.

In order to remove [a directory that contains] hidden items, PowerShell's Remove-Item cmdlet requires passing the -Force switch:
Remove-Item -Recurse -Force test
The shortest form of this command (for interactive rather than scripting use), using the platform-neutral ri alias and PowerShell's so-called elastic syntax, where it is sufficient to specify a prefix of a parameter name (such as -r for -Recurse), as long as that prefix is unambiguous):
ri -r -fo test
Note how a two-letter prefix is required for -Force, because -f alone is ambiguous: it could also refer to the -Filter parameter.
git init creates a .git subdirectory that is assigned the Hidden attribute on Windows (on Unix-like platforms, the fact that the name starts with . alone makes the directory a hidden one).
On Windows, rm, del, rmdir are simply built-in aliases of Remove-Item, the single cmdlet used to remove both files and directories (removal of the latter requiring -Recurse if the directory is not empty).
(On Unix-like platforms, only del is defined as an aliases, so as not to shadow the platform-native rm and rmdir utilities.)
ri is a platform-neutral alias derived from the name Remove-Item, and therefore preferable.
To see all aliases defined for a given command use Get-Alias -Definition $name; e.g.:
Get-Alias -Definition Remove-Item
Note: While it is arguably beneficial for PowerShell to require explicit opt-in via -Force in order to delete hidden items - so that you don't accidentally delete items whose existence you may not be aware of - the error message is suboptimal, in that the problem isn't one of permissions.

Related

rm binary in Windows $PATH but Powershell still prefers Remove-Item

I have the GNU core utils installed on Windows and have added the bin folder to my user PATH variable in Windows.
I have found that Powershell prefers Windows native commands over commands with the same name in PATH.
For example, when running get-command rm, powershell informs me that it's mapped to the internal
CommandType Name Version Source
----------- ---- ------- ------
Alias rm -> Remove-Item
Same with any commands where there is a Windows version - find, mkdir, etc.
Checking commands that I know Windows does not have an alternative for gives me the correct binay location
get-command md5sum
CommandType Name Version Source
----------- ---- ------- ------
Application md5sum.exe 5.3.0.1936 C:\Users\alshd\bin\coreutils\bin\md5sum.exe
Is there a way to tell PowerShell to prefer the versions I have specified in my PATH over the native Windows commands (without manually aliasing them)?
Building on Lee Dailey's helpful comments:
PowerShell aliases have higher precedence than external programs with the same (base) name.
Note that the precedence rules imply that in the absence of an rm alias a (hypothetical) rm function would take precedence too (and even a cmdlet, though the verb-noun naming convention of cmdlets makes this highly unlikely).
To see all command forms that exist for a given name, use Get-Command with the -All switch: Get-Command rm -All
To bypass naming conflicts, invoke the external program with its filename extension, namely rm.exe in this case; using the program's (full) path works too.
While it is advisable for scripts and modules to not use aliases - to put it differently: it is advisable to use aliases only as an interactive convenience - in practice they do, so there's a risk of breaking code if you remove built-in aliases such as rm.
If you're willing to assume this risk, see below for how to remove the rm alias; if you want the removal to apply to all future PowerShell sessions too, add the call to your $PROFILE file.
To remove the rm alias, using Remove-Item with the standard Alias: drive provided by the alias provider:
Remove-Item Alias:rm -Force
Note:
In PowerShell (Core) 6+, you can more simply use Remove-Alias rm -Force.
Not all aliases require -Force for removal, but many built-in ones do (though not rm, specifically).

How can I undo the last cd command on windows?

I want to go back to my previous location just like what cd - does on most unix shells.
How is this done on windows ?
You don't have cd - natively (cmd, powershell) on windows.
You can either:
1) Have cygwin on your windows and use bash
2) Have Linux subsystem on your Win 10 and use bash there.
3) use pushd on the directory you want to save. Then change somewhere you want.
To return back you just popd
First and Second Edit
There is a my own workaround with Powershell (licence MIT, but have my SO 'nick' there).
It will work the following way:
`cd` ... to list stack of saved directories
`cd -` ... return to previous directory (putting another `cd -` goes further to the history)
`cd +` ... add current directory to the top of the stack
`cd <path>` ... changes the path and puts the previous to the stack
You could put the following code in your Profile.ps1
# to remove default alias
Remove-Item Alias:cd -force
# create a new named stack at your ~ dir
Push-Location ~ -StackName 'RememberPaths'
# create a new cd with stack
function cd_with_stack {
<#
.SYNOPSIS
Change directory (cd)replacement
.DESCRIPTION
Remembers history when changing directories. When directory is changed then it is added to the stack
User can return to the history;
User can force manually adding a directory;
User can view the whole history stack.
.PARAMETER parameter
Can be - ... to return to the historical directory
Can be + ... to add current directory to the stack
Can be empty ... to view the whole stack
Can contain directory path to change it
.EXAMPLE
PS c:\users > cd -
PS c:\ >
PS C:\t> cd
Path
----
C:\t
C:\users\admin
C:\users
C:\Users\tukan
.NOTES
Author:
Tukan #StackOverflow
Version Info:
1.0 - 27/02/2018
- Initial release
.LINK
http://www.ceq.cz
#>
param ([String]$parameter)
switch ($parameter) {
'-' {
If ((Get-location -StackName 'RememberPaths').Count -gt 1) {
popd -StackName 'RememberPaths'
} Else {
# do not empty stack, always keep the first item on stack
Set-Location -Path ((Get-location -StackName 'RememberPaths').Peek().ToString())
}
break
}
'+' {
If ((Get-location -StackName 'RememberPaths').Peek().ToString() -ne (Convert-Path .)) {
pushd -StackName 'RememberPaths'
}
# Else -> no path storing to stack
break
}
'' { get-location -StackName 'RememberPaths'; break }
default {
If (Test-Path -Path $parameter) {
If ((Get-location -StackName 'RememberPaths').Peek().ToString() -eq (Convert-Path .)) {
Set-Location -Path $parameter
} Else {
pushd -Path $parameter -StackName 'RememberPaths'
}
} Else {
Write-Warning "Probably wrong path $parameter"
}
break
}
}
}
# setting new alias for cd
set-alias cd cd_with_stack
Details about commands:
Based on that comment below the question I'm adding this information. Which shows only a partial information.
With the pushd and popd it is important to differentiate when using cmd shell or powershell
In powershell - these two commands are aliases to different commands
PS C:\Users> get-alias -name pushd
CommandType Name ModuleName
----------- ---- ----------
Alias pushd -> Push-Location
PS C:\Users> get-help pushd
NAME
Push-Location
SYNTAX
Push-Location [[-Path] <string>] [-PassThru] [-StackName <string>] [-UseTransaction] [<CommonParameters>]
Push-Location [-LiteralPath <string>] [-PassThru] [-StackName <string>] [-UseTransaction] [<CommonParameters>]
ALIASES
pushd
REMARKS
Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
-- To download and install Help files for the module that includes this cmdlet, use Update-Help.
-- To view the Help topic for this cmdlet online, type: "Get-Help Push-Location -Online" or
go to http://go.microsoft.com/fwlink/?LinkID=113370.
PS C:\Users> get-alias -name popd
CommandType Name ModuleName
----------- ---- ----------
Alias popd -> Pop-Location
PS C:\Users> get-help popd
NAME
Pop-Location
SYNTAX
Pop-Location [-PassThru] [-StackName <string>] [-UseTransaction] [<CommonParameters>]
ALIASES
popd
REMARKS
Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
-- To download and install Help files for the module that includes this cmdlet, use Update-Help.
-- To view the Help topic for this cmdlet online, type: "Get-Help Pop-Location -Online" or
go to http://go.microsoft.com/fwlink/?LinkID=113369.
In cmd.exe (or command.com) the pushd and popd are internal commands of the interpreter:
C:\Windows\system32>pushd /?
Stores the current directory for use by the POPD command, then
changes to the specified directory.
PUSHD [path | ..]
path Specifies the directory to make the current directory.
If Command Extensions are enabled the PUSHD command accepts
network paths in addition to the normal drive letter and path.
If a network path is specified, PUSHD will create a temporary
drive letter that points to that specified network resource and
then change the current drive and directory, using the newly
defined drive letter. Temporary drive letters are allocated from
Z: on down, using the first unused drive letter found.
C:\Windows\system32>pushd /?
Stores the current directory for use by the POPD command, then
changes to the specified directory.
PUSHD [path | ..]
path Specifies the directory to make the current directory.
If Command Extensions are enabled the PUSHD command accepts
network paths in addition to the normal drive letter and path.
If a network path is specified, PUSHD will create a temporary
drive letter that points to that specified network resource and
then change the current drive and directory, using the newly
defined drive letter. Temporary drive letters are allocated from
Z: on down, using the first unused drive letter found.
It's true that you do not have cd- natively on Windows.
BUT you can use cd.. to go back one directory.
C:\Users\Acer\cd..
Will send you back to C:Users\ directory.
I know this answer is late, but again may be helpful for someone else.
Thanks!

how do I find all exe files using command line for windows?

I'm a newbie. I am trying to figure out how to use the command line. Please could you tell me what command I should enter so that I can get a list of all the exe files on my computer. thanks.
You can use the dir functionality to search the directory and all of its children directories while filtering on a particular file type.
dir /s /b *.exe | findstr /v .exe.
Source
If you want to find all the executable files that are on the path and/or in the current directory, i.e., all the files you can run from the command line without specifying a path, this should work:
where *.exe
To get names of all .exe files , that are currently running then type tasklist in cmd.
http://ss64.com/nt/tasklist.html
Here's another method I use a lot for tasks like this.
Open powershell and navigate to your root directory by entering the command
cd c:/
cd stands for change directory, and is an alias for the command "Set-Location". We are setting the location to C:/
Next run the following command:
Get-ChildItem -Filter "*.exe" -Recurse
Get-ChildItem is a function that gets the files and folders in a file system drive, and runs on whatever directory you're current at by default.
-Filter "*.exe" is an argument that specifies to only find filenames which end in ".exe". (The * is a type of regular expression notation).
-Recurse is an argument that specifies to search all child directories. This will make your function run on "C:/", but also all child directories of C:/, and all child directories of those directories and so on. This will allow you to search the entire drive.

Powershell parameter error 'p'

I tried typing this command
mkdir -p lol\hit\xx
But I get an error message
mkdir: Parameter cannot be processed because the parameter name 'p' is ambiguous.
I am following a tutorial online and according to that, there shouldn't be any error.
What is the reason behind this?
mkdir, when run in PowerShell, runs as an alias to New-Item. This can be seen by running Get-Help mkdir within PowerShell.
In that case -p is ambiguous because it could be either of the -Path or -PipelineVariable arguments for New-Item. I believe that what you want is:
mkdir -path lol\hit\xx
That will create the lol folder at your current location, and the hit folder inside it, and the xx folder inside of that.
The -p switch for mkdir in Unix forces the command to create all folders needed to get to the path you designate (so if all you had was 'lol' it would creates the 'hit' folder within that, and then create the 'xx' folder within the 'hit' folder). PowerShell's New-Item does this by default.

Renaming a file and remove 'dot' and replace it with' _'

I have set of files in a folder with name like abcd.15678
I want to remove the . and replace it with _
Pls suggest the windows command to do this
This solution is reposted from How to Batch Rename Files in Windows: 4 Ways to Rename Multiple Files by Chris Hoffman
PowerShell offers much more flexibility for renaming files in a command-line environment. Using PowerShell, you can pipe the output of one command – known as a “commandlet” in PowerShell terms — to another command, just like you can on Linux and other UNIX-like systems.
First of all, open Powershell ISE and then navigate to the directory (folder) that has the files and folders you'd like to rename by using this command:
cd "C:\your\directory\"
The two important commands you’ll need are Dir, which lists the files in the current directory, and Rename-Item, which renames an item (a file, in this case). Pipe the output of Dir to Rename-Item and you’re in business.
After you launch PowerShell ISE, use the cd command to enter the directory containing your files. You should put the files in their own directory so you don’t accidentally rename other files.
For example, let’s say we don’t want the dot character in our file names – we’d rather have an underscore instead.
The following command lists the files in the current directory and pipes the list to Rename-Item. Rename-Item replaces each dot character with an underscore.
Dir | Rename-Item –NewName { $_.name –replace ".","_" }
Consult Microsoft’s documentation on the Rename-Item commandlet if you want help performing other, more advanced operations.
There isn't a windows command to do this. You should consider writing a script of some sort that obtains a directory listing and enumerates through each entry: changes the dot to an underscore, and calls the windows rename command appropriately.
Actually this should work :
Dir | Rename-Item –NewName { $_.Name.Replace(".","_") }

Resources