PowerShell mklink on multiple files with regular expressions - powershell

I have a bunch of folders with similar prefixes in the name, and I'd like to make symbolick links to these folders so that I can remove the prefix while respecting the original folder naming convention. Here are some example folder names:
2013.Folder1
2013.Folder2
2014.Folder1
in the folder:
C:\Users\madeupname\Documents
In linux, I'd just do
ln -s /home/madeupname/Documents/201* /home/madeupname/Documents/links/
(this code may not exactly right as I don't have a linux box handy right now)
In Windows PowerShell, I could do it manually for these 3 files:
cmd /c mklink C:\Users\madeupname\Documents\links\2013.Folder1 C:\Users\madeupname\Documents\2013.Folder1
but that is no good because the real directory has a lot of files!

if I understood correctly this can work for you:
$path = "C:\Users\madeupname\Documents"
dir $path -Directory |
% { cmd /c mklink C:\Users\madeupname\Documents\links\$_.name $_.fullname /d}

I found a GUI to do this, but that's cheating so I won't mark this as the answer:
http://schinagl.priv.at/nt/hardlinkshellext/hardlinkshellext.html

Related

Delete file if it exists anywhere on the windows Device

I Am trying to delete a file which exists in like many folders which are dynamically created and I have no clue how and where to start. What would be the best approach using scripting to delete this file ?
del has a /s switch to process all subfolders:
del /s c:\googledrivesync.exe
it may take a while to scan the folder tree. Get yourself a cup of coffee...
Assuming you want to delete specific files in a directory structure (as opposed to just deleting everything), from PowerShell, you could do
dir -recurse -file -filter nameOfFileToRemove rootDirectory | remove-item
This will recurse through the file tree, looking for files matching nameOfFileToRemove and then removing them. Also nameOfFileToRemove may contain wildcard characters like "nameOfFile*.txt"

Change directory in PowerShell

My PowerShell prompt's currently pointed to my C drive (PS C:\>). How do I change directory to a folder on my Q (PS Q:\>) drive?
The folder name on my Q drive is "My Test Folder".
Unlike the CMD.EXE CHDIR or CD command, the PowerShell Set-Location cmdlet will change drive and directory, both. Get-Help Set-Location -Full will get you more detailed information on Set-Location, but the basic usage would be
PS C:\> Set-Location -Path Q:\MyDir
PS Q:\MyDir>
By default in PowerShell, CD and CHDIR are alias for Set-Location.
(Asad reminded me in the comments that if the path contains spaces, it must be enclosed in quotes.)
To go directly to that folder, you can use the Set-Location cmdlet or cd alias:
Set-Location "Q:\My Test Folder"
Multiple posted answer here, but probably this can help who is newly using PowerShell
SO if any space is there in your directory path do not forgot to add double inverted commas "".
You can simply type Q: and that should solve your problem.
Set-Location -Path 'Q:\MyDir'
In PowerShell cd = Set-Location
You can also use the sl command to be able to change directories. It is Set-Location but it is much shorter.
Example:
# Too verbose
Set-Location -Path C:\
# Just the right amount of characters to type
sl C:\
If your Folder inside a Drive contains spaces In Power Shell you can Simply Type the command then drive name and folder name within Single Quotes(''):
Set-Location -Path 'E:\FOLDER NAME'
The Screenshot is attached here
On Powershell use Set-Location instead of cd.
Put path in quotes. Single quotes works for me.
Set-Location 'C:\Program Files\MongoDB\Server\6.0'

What is DIR command a substitute of in PowerShell

I know that I can use DIR to list the directory and MKDIR to create a new one. However, I learned today that one can (should?) use New-Item -ItemType Directory "c:\pip" instead, which, indeed, looks much more PowerShellish.
I'm not getting any hits when googling for the equivalent of DIR, though. Is DIR just DIR?
dir | ls == get-childitem.
You can figure out aliases using the get-alias command: get-alias dir. Aliases are used fairly frequently, for example, gci is an alias for get-childitem as well.

How to use 'dir' to different folders in one command?

Any ideas how to use the command 'dir' to more than 1 folder in one command?
dir c:\A | c:\test\B
is not working. Im not good with pipes...
Use the Get-ChildItem cmdlet instead, or use DIR and separate your sources by commas, not spaces. DIR doesn't work the same in PowerShell as it does in a DOS window.
http://technet.microsoft.com/en-us/library/ee176841.aspx
dir c:\A c:\test\B should work. No pipe necessary.

PowerShell analog to "dir /a:d" (Win) or "ls -d */" (Bash)

I simply want to list all of the directories under my current working directory, using PowerShell. This is easy from a Bash shell:
ls -d */
or cmd.exe in Windows:
dir /a:d
Using PowerShell however I cannot seem to be able to do it with a single command. Instead the only the I've found that works is:
ls | ? {$_Mode -like "d*"}
That seems way too wordy and involved, and I suspect that I don't need a separate Where clause there. The help for Get-ChildItem doesn't make it clear how to filter on Mode though. Can anyone enlighten me?
This works too:
ls | ?{$_.PsIsContainer}
There is no doubt that it is a little more wordy than bash or cmd.exe. You could certainly put a function and an alias in your profile if you wanted to reduce the verbosity. I'll see if I can find a way to use -filter too.
On further investigation, I don't believe there is a more terse way to do this short of creating your own function and alias. You could put this in your profile:
function Get-ChildContainer
{
param(
$root = "."
)
Get-ChildItem -path $root | Where-Object{$_.PsIsContainer}
}
New-Alias -Name gcc -value Get-ChildContainer -force
Then to ls the directories in a folder:
gcc C:\
This solution would be a little limited since it would not handle any fanciness like -Include, -Exclude, -Filter, -Recurse, etc. but you could easily add that to the function.
Actually, this is a rather naive solution, but hopefully it will head you in the right direction if you decide to pursue it. To be honest with you though I wouldn't bother. The extra verbosity in this one case is more than overcome by the overall greater flexibility of powershell in general in my personal opinion.
Try:
ls | ? {$_.PsIsContainer}
dir -Exclude *.*
I find this easier to remember than
dir | ? {$_.PsIsContainer}
Plus, it is faster to type, as you can do -ex instead of -exclude or use tab to expand it.
You can now use Get-ChildItem -Directory or ls -dir for short. This has existed at least since PowerShell 3.0 according to Microsoft's documentation.
You can check old post on PowerShell team blog: http://blogs.msdn.com/powershell/archive/2009/03/13/dir-a-d.aspx
I came to this thread because I'm trying to figure out how "FOR /D" works. Well actually how to use the batch-command escape(%) with the /D option.
I read the above items with hope, to be honest they're all a lot more complex than the FOR command option -- If it worked of course.
Using additional forms of for
If command extensions are enabled (that is, the default), the following additional forms of for are supported:
Directories only
If set contains wildcards (* and ?), the specified command executes for each directory (instead of a set of files in a specified directory) that matches set. The syntax is:
for /D {%% | %}variable in (set) do command [CommandLineOptions]
Well, soon we will have to a make a class to simplify things and be typing commands like
dir -options {122b312aa3132-1313-131112f8111111} just so we can do the same as
dir/ad/od