Current folder structure:
\\Server\Source\A1\A1 101\
\\Server\Source\A1\A1 102\
\\Server\Source\A2\A2 101\
\\Server\Source\A2\A2 102\
\\Server\Source\A3\A3 101\
\\Server\Source\A3\A3 102\
I need to be able to create 3 folders, A, B, and C, inside each of the second level folders under source (A1 101, A2 101, etc.). FolderDir.txt has A, B, C on their own line. Is there a better way other than the following? There are a LOT more sub directories than listed above:
Foreach($folder in Get-Content "C:\temp\folderDir.txt")
{
New-Item "\\Server\Source\A1\A1 101\$folder" -ItemType directory
New-Item "\\Server\Source\A1\A1 102\$folder" -ItemType directory
New-Item "\\Server\Source\A2\A2 101\$folder" -ItemType directory
New-Item "\\Server\Source\A2\A2 102\$folder" -ItemType directory
New-Item "\\Server\Source\A3\A3 101\$folder" -ItemType directory
New-Item "\\Server\Source\A3\A3 102\$folder" -ItemType directory
}
Thanks! I'm still pretty new to PowerShell.
This is what I came up with:
Foreach($folder in Get-Content "C:\temp\folderDir.txt")
{
$lesson = Get-ChildItem -Path \\Server\Source\A*\*
New-Item $lesson\$folder -ItemType Directory
}
But I'm getting the error:
New-Item : Could not find a part of the path 'A'.
At line:4 char:1
+ New-Item $lesson\$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\\Server\Source...\A:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand
New-Item : Could not find a part of the path 'B'.
At line:4 char:1
+ New-Item $lesson\$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\\Server\Source...\B:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand
New-Item : Could not find a part of the path 'C'.
At line:4 char:1
+ New-Item $lesson\$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\\Server\Source...\C:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand
However, if I put -WhatIf command on the end (after "directory") it appears to be able to do what I want...
Because the folderlist is iterated over several times it should be read in first
untested:
$folderlist = Get-Content "C:\temp\folderDir.txt"
Get-ChildItem -Path '\\Server\Source\*\*' -Directory | ForEach-Object {
ForEach ($folder in $folderlist){
New-Item (Join-Path $_.FullName $folder) -ItemType directory -Force -WhatIf
}
}
If the output look OK remove the -WhatIf
Related
So I'm running a script that creates folders from a list of usernames in a CSV and in those folders it creates a folder called "Documents" and I'm then trying to give "modify" access rights to the "Documents" folder for the user concerned - eg a folder is created for john.smith, inside that folder is a "Documents" folder, and the user john.smith gets modify access rights to that "Documents" folder.
Creating the folders works without a hitch, it's the access rights part that is giving me a headache - it returns an Invalid Argument error for the last line of the code below and I can't figure out why - any help would be appreciated.
$Location = "C:\Scripts"
Set-Location $Location
$Folders = Import-Csv "C:\Scripts\UserFolderList.csv"
ForEach ($Folder in $Folders)
{
#Create A Folder From The "Name" Column In The CSV Then Create A Subfolder Called Documents
New-Item $Folder.Name -ItemType Directory
$Docs = "Documents"
$DocsPath = Join-Path $Folder.Name $Docs
New-Item -Path $DocsPath -ItemType Directory
#Assign The Modify Permission For The Username To The Documents Folder
$Domain = "tly"
$DomainUser = $Domain + $Folder.Name
$FullDocsPath = "$Location" + $Folder.Name + "$Docs"
$Acl = Get-Acl $FullDocsPath
$ArgumentList="$DomainUser","Modify","ContainerInherit,ObjectInherit","None","Allow"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($ArgumentList)
$Acl.SetAccessRule($AccessRule)
Set-Acl -path $FullDocsPath -aclObject $AccessRule
}
The resulting error is:
Set-Acl : AclObject
At line:22 char:1
Set-Acl -path $FullDocsPath -aclObject $AccessRule
CategoryInfo : InvalidArgument: (System.Security...ystemAccessRule:FileSystemAccessRule) [Set-Acl], ArgumentException
FullyQualifiedErrorId : SetAcl_AclObject,Microsoft.PowerShell.Commands.SetAclCommand
Since you can only view the C:\$RECYCLE.BIN Folder when you uncheck (Hide protected Operating System Files) you can't change the date modified through the usual way with PowerShell:
$file = Get-Item C:\Path\TO\File.txt
$file.LastWriteTime = (Get-Date)
or
$folder = Get-Item C:\folder1
$folder.LastWriteTime = (Get-Date)
When calling Get-Item C:\$RECYCLE.BIN, I get the following error:
Get-Item : Cannot find path 'C:\.bin' because it does not exist.
At line:1 char:9
+ $file = Get-Item C:\$Recycle.bin
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\.bin:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
How would I go about changing the date modified of it?
As C:\$RECYCLE.BIN contains $, PowerShell tries to evaluate the variable $RECYCLE, which is empty. Thus, the resulting path C:\.bin cannot be found. Quote your path with single quotes (') to prevent PowerShell from evaluating your string and also add the -Force parameter as it is a system directory:
Get-Item -Path 'C:\$RECYCLE.BIN' -Force
You can then access your files and change the LastWriteTime as you already tried in your question:
$fileObject = Get-Item -Path 'C:\$RECYCLE.BIN\S-1-5-21-3323847664-626704455-790384891-1001\$RT8USDF.txt' -Force
$fileObject.LastWriteTime = Get-Date
Here's the code and the error I get:
function CSheridanStruct {
New-Item -Path "C:\Users\Admininistrator\" -Name "Sheridan" -ItemType "directory" |
New-Item -Path "C:\Users\Admininistrators\Sheridan\" -ItemType "directory" -Name "SYST23551", "Notes"
}
Set-Alias Sheridan CSheridanStruct
Sheridan
New-Item : Cannot convert 'System.Object[]' to the type 'System.String'
required by parameter 'Name'. Specified method is not supported.
At line:2 char:167
+ ... rators\Sheridan\" -ItemType "directory" -Name "SYST23551", "Notes" }
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewItemCommand
I also tried it without the pipline on separate lines (this is within the function) tried just Set-Alias Sheridan CSheridanStruct same errors.
And I tried to do Set-Alias -Name "Sheridan" -Value CSheridanStruct.
Same output. The functions commands within, I've already checked and work and have created the directories. I just need to set an alias for all the commands to launch at once with typing the alias Sheridan in PowerShell..
This issue you are having is trying to put a array in the new-item -name field
New-Item -Path "C:\Users\Admininistrators\Sheridan\" -ItemType "directory" -Name "SYST23551", "Notes"
The error you are getting is because of -Name "SYST23551", "Notes"
Second there is no need to pipe these commands as they have nothing to do with each other
Here is a working version of your script
function CSheridanStruct {
New-Item -Path "C:\Users\Admininistrator\" -Name "Sheridan" -ItemType "directory"
New-Item -Path "C:\Users\Admininistrator\Sheridan" -Name "SYST23551" -ItemType "directory"
New-Item -Path "C:\Users\Admininistrator\Sheridan" -Name "Notes" -ItemType "directory"
}
Set-Alias Sheridan CSheridanStruct
Sheridan
One of my scripts is to move Google Chrome bookmarks from one machine on the network to another. When I run the script, it appears to run and then throws a Copy-Item error. Pretty sure there's something wrong with my file path, but I can't figure out what it is.
Code first, error follows:
[CmdletBinding()]
param(
[String]$SourceComputerName,
[String]$DestinationComputerName,
[String]$UserName
)
BEGIN {
$ErrorActionPreference = 'Continue'
$CMDOUT=#{ #This is used so that the function accepts -Verbose / -Debug parameters
Verbose=If ($PSBoundParameters.Verbose -eq $true) { $true } else { $false };
Debug=If ($PSBoundParameters.Debug -eq $true) { $true } else { $false }
}
$SourcePath = '\\' + $SourceComputerName + '\C$\users\' + $UserName + '\AppData\Local\Google\Chrome\User Data\Default' #Transfer Bookmarks from here
$DestinationPath = '\\' + $DestinationComputerName + '\C$\users\' + $UserName + '\AppData\Local\Google\Chrome\User Data\Default' #Transfer Bookmarks to here
}
PROCESS{
#Create copy of destination backup file, make sure not to overwrite other backup files that might exsist
If(Test-Path ($DestinationPath + '\Bookmarks')){
$i = 1
do{
$BackupPath = ($DestinationPath + '\Bookmarks') + '_' + $i.ToString('000') + '.old'
$i++
}
while(Test-Path $BackupPath)
Copy-Item -Path ($SourcePath + '\Bookmarks') -Destination $BackupPath #CMDOUT
}
#Copy Bookmarks to DestinationComputer
Copy-Item -Path ($SourcePath + '\Bookmarks') -Destination ($DestinationPath + '\Bookmarks') -Force #CMDOUT
#Replace the Chrome create Bookmark backup (.bak) file
Copy-Item -Path ($SourcePath + '\Bookmarks') -Destination ($DestinationPath + '\Bookmarks.bak') -Force #CMDOUT
#Copy Favicons (for bookmarks)
If(Test-Path ($DestinationPath + '\Favicons')){
Copy-Item -Path ($SourcePath + '\Favicons') -Destination ($DestinationPath + '\Favicons') -Force #CMDOUT
}
}
END{
}
}
Copy-Item : The network path was not found
At C:\Users\Shane.goldsberry\PowerShellScripts\PS1\MigrateChromeBookmarks.ps1:61 char:9
Copy-Item -Path ($SourcePath + '\Bookmarks') -Destination ($D ...
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : The network path was not found At C:\Users\Shane.goldsberry\PowerShellScripts\PS1\MigrateChromeBookmarks.ps1:63 char:9 Copy-Item -Path ($SourcePath + '\Bookmarks') -Destination ($D ...
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
I'm new to PowerShell and I can't seem to find how to fix this after countless Google searches. I know it's probably easy, but here's basically what I want to do and the error that shows:
PS C:\Windows\system32> $path = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"
Get-Childitem $path -ErrorAction SilentlyContinue | Foreach {
$key = Get-Item $_.PSPath
if($key.Property -eq "VMnet") {
New-ItemProperty $key -name "*NdisDeviceType" -value "1"
}
}
New-ItemProperty : Cannot find path 'C:\Windows\system32\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0014' because it does not exist.
At line:7 char:25
+ New-ItemProperty <<<< $key -name "*NdisDeviceType" -value "1"
+ CategoryInfo : ObjectNotFound: (C:\Windows\syst...02BE10318}\0014:String) [New-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.NewItemPropertyCommand
New-ItemProperty : Cannot find path 'C:\Windows\system32\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0015' because it does not exist.
At line:7 char:25
+ New-ItemProperty <<<< $key -name "*NdisDeviceType" -value "1"
+ CategoryInfo : ObjectNotFound: (C:\Windows\syst...02BE10318}\0015:String) [New-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.NewItemPropertyCommand
I clearly understand the error, it's obvious. But I don't know the proper way/command to fix it...
Try this:
$path = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\" +
"{4D36E972-E325-11CE-BFC1-08002BE10318}"
Get-Childitem $path -ErrorAction SilentlyContinue |
Where {(Get-ItemProperty $_.PSPath DriverDesc) -match 'VMnet' } |
Foreach {
New-ItemProperty $_.PSPath -name "*NdisDeviceType" -value "1"
}
}
BTW I don't see any regkeys for values named "Property" perhaps you could match on the DriverDesc reg value? Anyway, the reason you're getting the error is you have to specify the PSPath to New-ItemProperty ie in your script $key.PSPath.