I have a problem that I cannot seem to find any solution to online. I am trying to restructure some data structures that I ended up with after exporting some data from my old database service.
The export left me with a bunch of client folders in the format of [Client Name] -> Archive -> Archive -> [Client Files/Subfolders]. What I would like to end up with is [Client Name] -> [Client Files/Subfolders], removing the two redundant "Archive" folders in between.
Many people have suggested to me that I simply cut and paste the files from the subfolder to the client root directory, however with hundreds of client folders this would be a very long task to do. Would there be any batch cmd command or powershell command I could use to work through these folders and restructure them? Any advice is much appreciated.
Three built-in PoSH cmdlets are there for exactly these purposes.
Get-ChildItem (aka gci, dir and ls)
(Get-Command -Name Get-ChildItem).Parameters.Keys
Get-Help -Name Get-ChildItem -Full
Get-Help -Name Get-ChildItem -Examples
Move-Item (aka mi, move, and mv)
(Get-Command -Name Move-Item).Parameters.Keys
Get-Help -Name Move-Item -Full
Get-Help -Name Move-Item -Examples
Remove-Item (aka del, erase, rd, ri, rm, rmdir)
(Get-Command -Name Remove-Item).Parameters.Keys
Get-Help -Name Remove-Item -Full
Get-Help -Name Remove-Item -Examples
See details here:
Files and Folders in Windows PowerShell
We’ll start out by telling you that everything you learned last month about manipulating files and folders with WMI will work in Windows PowerShell. PowerShell has a cmdlet called Get-WMIObject that allows you to access all the classes, methods and properties of WMI. Just like with VBScript, WMI is the only way to work with files and folders remotely in Windows PowerShell. We’re not going to get into working with WMI in Windows PowerShell in this article, but we’re going to show you the PowerShell way to work with files and folders.
https://technet.microsoft.com/en-us/library/ee176988.aspx
As for this...
"removing the two redundant "Archive" folders in between."
If there is nothing in these folders of value, then the above Remove-Item should be all you need. Otherwise you move all files from the to wherever, then remove the folders.
Or just use robocopy
https://technet.microsoft.com/en-us/library/cc733145(v=ws.11).aspx
https://social.technet.microsoft.com/wiki/contents/articles/1073.robocopy-and-a-few-examples.aspx
Robocopy (Robust File Copy) is a command-line file copy utility that comes with Windows Vista / Windows 2008 or newer. Until Vista, Robocopy was a part of Windows Resource Kit Tools as a free download (http://aka.ms/robocopydownload Jump ). Unlike normal copy commands, Robocopy is designed for reliable copy or mirroring while maintaining the permissions, attributes, owner information, timestamps and properties of the objects copied.
Related
I just started working with PowerShell and I want to know how to transfer files between a local server and a remote server.
Currently I do not have Administrator access to either server (I know I'm going to need it), how do I target the Get cmdlt? Do I use a URL?
It's vital that you get up to speed on the topic. Take the time to jump over to MS Virtual academy
or MS Channel9
or YouTube and take a quick training course on the topic.
What you are asking for is covered in them as well as in the PowerShell Help files, which include examples on how to do this.
# get function / cmdlet details
(Get-Command -Name Copy-Item).Parameters
Get-help -Name Copy-Item -Full
Get-help -Name Copy-Item -Online
Get-help -Name Copy-Item -Examples
NAME
Copy-Item
SYNOPSIS
Copies an item from one location to another.
Example 1: Copy a file to the specified directory
PS C:\>Copy-Item "C:\Wabash\Logfiles\mar1604.log.txt" -Destination "C:\Presentation"
This command copies the mar1604.log.txt file to the C:\Presentation directory. The command does not delete the original
file.
Example 2: Copy the contents of a directory to another directory
PS C:\>Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse
This command copies the entire contents of the Logfiles directory into the Drawings directory. If the LogFiles directory contains files
in
subdirectories, those subdirectories will be copied with their file trees intact. The Container parameter is set to true by default.
This preserves
# Get parameter that accepts pipeline input
Get-Help Copy-Item -Parameter * |
Where-Object {$_.pipelineInput -match 'true'} |
Select *
# Get cmdlet / function parameter aliases
(Get-Command Copy-Item).Parameters.Values |
where aliases |
select Name, Aliases | Out-GridView -PassThru -Title 'Alias results for a given cmdlet or function.'
I have a large list of folders within a single folder directory. My network rules are set where ntfs permissions are not inherited. I need to assign read and write permissions to each individual folder for the same 4 users to each of these individual folders is there a script for this?
Thanks for your assistance!
All you need is to get to your goal is here:
# Get parameters, examples, full and Online help for a cmdlet or function
(Get-Command -Name Get-Acl).Parameters
Get-help -Name Get-Acl -Examples
Get-help -Name Get-Acl -Full
Get-help -Name Get-Acl -Online
(Get-Command -Name Set-Acl).Parameters
Get-help -Name Set-Acl -Examples
Get-help -Name Set-Acl -Full
Get-help -Name Set-Acl -Online
(Get-Command -Name Get-NTFSAccess).Parameters
Get-help -Name Get-NTFSAccess -Examples
Get-help -Name Get-NTFSAccess -Full
Get-help -Name Get-NTFSAccess -Online
(Get-Command -Name Add-NTFSAccess).Parameters
Get-help -Name Add-NTFSAccess -Examples
Get-help -Name Add-NTFSAccess -Full
Get-help -Name Add-NTFSAccess -Online
PowerShell – Editing permissions on a file or folder
I've been trying to figure out how to change permissions on a folder in PowerShell. I've looked at the Get-Acl and Set-Acl, but I can only use them to copy the settings from a pre-existing object. How do I manually configure permissions?
This is actually a quite common question, so I thought I'd write a quick post on the subject.
https://blogs.msdn.microsoft.com/johan/2008/10/01/powershell-editing-permissions-on-a-file-or-folder
Weekend Scripter: Use PowerShell to Get, Add, and Remove NTFS Permissions
Managing file and folder permissions in Windows PowerShell is not that easy, and there are numerous articles and blog posts describing how it works by using the .NET classes. This is far from being comfortable, and there is one major and one minor restriction:
• Path length
• Generic rights
This post introduces the NTFSSecurity module, which provides a bunch of cmdlets for managing permissions on NTFS drives. It does not use the Windows PowerShell way to access the file system, and it works around the MAX_PATH, which is 260 characters. (For more information, see Naming Files, Paths, and Namespaces). This is achieved thanks to AlphaFS.
https://blogs.technet.microsoft.com/heyscriptingguy/2014/11/22/weekend-scripter-use-powershell-to-get-add-and-remove-ntfs-permissions
Given the Microsoft documentation for PowerShell, I can see no reason why the following code should fail with the given error. Then again, PowerShell can fail when a script just gets too long. All the paths are double-quote strings.
##### ALGORITHM Take in keystore path, make a backup in an adjacent directory
$ksPath = $java_store_path.Substring(0, $java_store_path.LastIndexOf('\') + 1)
$backupPath = $ksPath + "backups"
New-Item $backupPath PowerShell -type directory -force
New-Item : A positional parameter cannot be found that accepts argument 'PowerShell'.
https://technet.microsoft.com/en-us/library/ee176914.aspx
New-Item c:\scripts\Windows PowerShell -type directory
If that's valid, mine should be too. I'm running on Server 2012 R2.
The example on that page is just plain wrong. It seems they meant to refer to the path C:\Scripts\WindowsPowerShell or they forgot to quote the directory with spaces in it.
So it should have been one of these:
New-Item c:\scripts\WindowsPowerShell -type directory
New-Item 'c:\scripts\Windows PowerShell' -type directory
New-Item "c:\scripts\Windows PowerShell" -type directory
Ask yourself, what would PowerShell alone have been referring to? What parameter would it have corresponded to?
Edit: as the commenters have pointed out, the example was supposed to show the nameSet parameters, where a separate -Path and -Name are specified, and purportedly PowerShell was supposed to be a value to the -Name parameter. That does look correct. The reason the example didn't work (and yours as well), is because the -Name parameter cannot be specified positionally, which you can see in the MSDN article I linked to below, and in the built-in help:
Type: String
Parameter Sets: nameSet
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
In that case, their example should have been something like these:
New-Item c:\scripts\Windows -Name PowerShell -type directory
New-Item -Path c:\scripts\Windows -Name PowerShell -type directory
So reiterating, named parameters would have worked here, and would have avoided confusion.
Generally, you shouldn't be using positional parameters in scripts, unless they're extremely clear (and even then, I'd recommend avoiding).
Using named parameters would have made this easier to figure out. And tab-completion helps with filling in the parameter names and in completing paths (usually with proper quoting too).
I think you should change yours to:
New-Item -Path $backupPath -Type Directory -Force
And looking over that technet article, it's really not so good. The MSDN article on New-Item is better, and this is the information you should see when running Get-Help New-Item as well.
Side question:
Then again, PowerShell can fail when a script just gets too long.
What?
With Unix cp you can use the --link option. When used with a folder, it will
hard link the files involved instead of
copying, example
cp --recursive --link foo bar
This can be ideal in certain situations because it is faster than regular
copying. Can anything like this be done with PowerShell?
PowerShell doesn't have support for Symbolic/Hard Links currently. There are improvements on this front coming in PowerShell 5.0. The latest preview (September 2014) includes some of this functionality. You may want to peruse the release notes (docx):
To support symbolic links, *-Item and a few related cmdlets have been extended. Now you can create symbolic links in a single, simple line with New-Item.
An example:
New-Item -ItemType HardLink -Path C:\Temp -Name MyHardLinkFile.txt -Value $pshome\profile.ps1
There isn't an example for Copy-Item, but I assume it would be simple to use this with a recursive Get-ChildItem and pipe the results to New-Item, but you would have to try it yourself.
In the meantime, the PowerShell Community Extensions project has a New-Hardlink cmdlet. From the looks of it, you would have to do as I described above and pipe the results of Get-ChildItem into this cmdlet to create a hardlink for each file.
I am trying to write a Powershell command to identify and copy only text (.txt) files from within a directory and sub-directories of that directory. Does anyone know if there is a command that can perform this task.
If you're not worried about maintaining the directory structure you could use
Get-ChildItem *.txt -recurse | Copy-Item -destination c:\qwerty
If you would like to maintain the directory structure you could use
Copy-Item -Recurse -Filter *.txt -path c:\temp -destination c:\asdf
NB.
The PowerShell get-help command is very useful and it does except wildcards.
ie
get-help *copy* gives you a list of commands that might be useful to you.
get-help Copy-Item -full gives you all the parameters plus examples of usage.