I am very new to powershell and situation is that I have some unneeded users folder that must be deleted on different servers. Path on all servers is f.e. "\server1\hiddenshare$\username" My CSV looks like:
ServerName,FolderName
SERVER1,AAAA
SERVER2,AAA1
SERVER3,AAA2
And I am trying to run this code:
$path = "C:\temp\servers_folders.csv"
$ServerName = Import-Csv -Path $path
$FolderName = Import-CSV -Path $path
Import-Csv -Path $path | % {
Remove-Item -Path \\$ServerName\hiddenshare$\$FolderName -Recurse
}
After all I get this error:
Remove-Item : Cannot find path '\\ \$\ ' because it does not exist.
At C:\temp\servers_folders.csv.ps1:5 char:3
+ Remove-Item -Path \\$ServerName\hiddenshare$\$FolderName -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\\ \usr$\ :String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
you dont need to import your csv more than once.
Inside the foreach block you have to reference the current item by $_
try this, remove -whatif if it's ok
$servers=import-csv .\servers.csv
$servers | %{
remove-item -path \\$($_.servername)\users$\$($_.FolderName) -whatif
}
Related
After running this script:
# Input path
$input_path = 'C:\Users\larry\Pictures\2003'
# Export file
$documents_export_file = 'C:\Users\larry\Documents\Temp\ffmpeg\fullnames.csv'
# Copy-to folder
$copy_to = "C:\Users\larry\Documents\Temp\ffmpeg\movies"
# List filenames in $documents_export_file
Get-ChildItem -Attributes !Directory -Path $input_path -Recurse -force | select-object -Property fullName | Export-Csv $documents_export_file
# Copy the files listed in $documents_export_file to $copy_to folder
get-content $documents_export_file | Foreach-Object { copy-item -Path $_ -Destination $copy_to}
I get the following error:
copy-item : Cannot find drive. A drive with the name '"C' does not exist.
At line:18 char:55
+ ... ort_file | Foreach-Object { copy-item -Path $_ -Destination $copy_to}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
copy-item : Cannot find drive. A drive with the name '"C' does not exist.
At line:18 char:55
+ ... ort_file | Foreach-Object { copy-item -Path $_ -Destination $copy_to}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
As far as I can figure out, it has something to do with my Export-Csv, bur I can´t figure it out.
Please give me a hand.
There's no need to export the list of files to CSV in the first place - assign the output from Get-ChildItem to a variable instead, and then pipe the contents directly to Copy-Item:
$files = Get-ChildItem -Attributes !Directory -Path $input_path -Recurse -Force
$files |Copy-Item -Destination $copy_to
i have to delete folders based on a excel list, so i have tried to import the execl with import csv but it dosent work.
The import doesnt fill the variable in the correct format.
This is the code i tried to use:
$folders = import-csv G:\Book1.csv foreach ($folder in $folders) {Remove-Item -Path $folder -Recurse -Force}
The error is this:
Remove-Item : Cannot find drive. A drive with the name '#{Foldername=G' does not exist.
At line:4 char:5
+ Remove-Item -Path $folder -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{Foldername=G:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot find drive. A drive with the name '#{Foldername=G' does not exist.
At line:4 char:5
+ Remove-Item -Path $folder -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{Foldername=G:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot find drive. A drive with the name '#{Foldername=G' does not exist.
At line:4 char:5
+ Remove-Item -Path $folder -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{Foldername=G:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
In the CSV in the 1.Line heading stands foldername in line 2-4 are the name of the folders (a, b, c).
This is the CSV:
Foldername
G:\Test\a
G:\Test\b
G:\Test\c
The directory is G:\Test\a , b, c
I have found a Solution, the CSV file was a Problem and the import so i had to modify the CSV File:
"Foldername"
"G:\Test\a"
"G:\Test\b"
"G:\Test\c"
the powershell code is this now:
$folders = import-csv .csv | ForEach-Object {
write-host $($_.Foldername)
Remove-Item -Path $($_.Foldername) -Recurse -Force
}
The following script runs great except for the fact that it takes days to finish. Does anyone have any tweaks or tips to cut down the execution time?
The shared directory, Images\Equipment, contains over 5500 folders is grows daily.
# If the Admin folder for Equipment Images does not exist, make a new one and set the correct permissions.
$Location = "E:\Images\Equipment\*\"
$file = "E:\Images\Equipment\*\Admin"
foreach ($_ in (Get-ChildItem E:\Images\Equipment\*\)){if(($_.PSIsContainer -AND $_.name -eq "Admin")-eq $false)
{
New-Item -Path $location -Name "Admin" -ItemType directory
$errorActionPreference = "continue"}}
$folder = "E:\Images\Equipment\*\Admin"
Get-ChildItem E:\Images\Equipment\ -Directory -Filter "admin" -Recurse | ForEach-Object {
$acl = Get-Acl $_.FullName
if ($acl.AreAccessRulesProtected) { $acl.Access | % {$acl.purgeaccessrules($_.IdentityReference)} }
else {
$isProtected = $true
$preserveInheritance = $false
$acl.SetAccessRuleProtection($isProtected, $preserveInheritance)
}
$account="recoequip\folder sales group"
$rights=[System.Security.AccessControl.FileSystemRights]::FullControl
$inheritance=[System.Security.AccessControl.InheritanceFlags]"ContainerInherit,ObjectInherit"
$propagation=[System.Security.AccessControl.PropagationFlags]::None
$allowdeny=[System.Security.AccessControl.AccessControlType]::Allow
$dirACE=New-Object System.Security.AccessControl.FileSystemAccessRule ($account,$rights,$inheritance,$propagation,$allowdeny)
$ACL.AddAccessRule($dirACE)
Set-Acl -aclobject $ACL -Path $folder
Write-Host $folder Permissions added}
Thank you for any assistance.
Sara
Ok, to try and speed up your script I would create the ACE once at the beginning as suggested by mjolinor in his comment above. I would not bother looking for folders that are missing an Admin subfolder, and just create the Admin sub-folder in each folder regardless, and use the -force argument. That won't delete and re-create, but what it will do is return a folder object for each folder even if it already existed. Collect all those folders in a variable. Then iterate through those folders and apply the correct permissions for them.
# If the Admin folder for Equipment Images does not exist, make a new one and set the correct permissions.
#Define ACE to apply
$account="recoequip\folder sales group"
$rights=[System.Security.AccessControl.FileSystemRights]::FullControl
$inheritance=[System.Security.AccessControl.InheritanceFlags]"ContainerInherit,ObjectInherit"
$propagation=[System.Security.AccessControl.PropagationFlags]::None
$allowdeny=[System.Security.AccessControl.AccessControlType]::Allow
$dirACE=New-Object System.Security.AccessControl.FileSystemAccessRule ($account,$rights,$inheritance,$propagation,$allowdeny)
#Make sure all Equipment Images folders have an 'Admin' subfolder, and store the folder objects for later
$Folders = Get-ChildItem E:\Images\Equipment\* -Directory | %{New-Item -Path ($_.FullName + "\Admin") -ItemType directory -ErrorAction Continue -Force}
ForEach($Folder in $Folders){
$acl = Get-Acl $Folder.FullName
if ($acl.AreAccessRulesProtected) { $acl.Access | % {$acl.purgeaccessrules($_.IdentityReference)} }
else {
$isProtected = $true
$preserveInheritance = $false
$acl.SetAccessRuleProtection($isProtected, $preserveInheritance)
}
$ACL.AddAccessRule($dirACE)
Set-Acl -aclobject $ACL -Path $folder
Write-Host $folder Permissions added
}
This does assume that you are using PSv3 or better and have access to the -Directory switch for the Get-ChildItem cmdlet. If you do not have v3 installed I would suggest upgrading your version of PowerShell. I realize that's not always an option so alternatively you can change line 12 to this:
$Folders = Get-ChildItem E:\Images\Equipment\* -Attributes Directory | %{New-Item -Path ($_.FullName + "\Admin") -ItemType directory -ErrorAction Continue -Force}
or
$Folders = Get-ChildItem E:\Images\Equipment\* | ?{$_.PSIsContainer} | %{New-Item -Path ($_.FullName + "\Admin") -ItemType directory -ErrorAction Continue -Force}
If you have to use one of these I would suggest the first one since that filters for only directories at the FileSystem Provider level, instead of returning all folders and files, and then making PowerShell filter out the files.
This was to long to add in a comment.
This is the error it gives repeatedly in between assigning permissions to certain Admin folders.
Get-Acl : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or
empty, and then try the command again.
At line:15 char:20
+ $acl = Get-Acl $_.FullName
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Acl], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetAclCommand
You cannot call a method on a null-valued expression.
At line:21 char:9
+ $acl.SetAccessRuleProtection($isProtected, $preserveInheritance)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:24 char:5
+ $ACL.AddAccessRule($dirACE)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Set-Acl : Cannot bind argument to parameter 'AclObject' because it is null.
At line:26 char:24
+ Set-Acl -aclobject $ACL -Path $folder
+ ~~~~
+ CategoryInfo : InvalidData: (:) [Set-Acl], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetAclCommand
$folderpath = 'E:\BOOKS\Python\python\python'
$items = Get-ChildItem -Recurse $folderpathc *_pdf
foreach( $i in $items) { Rename-Item E:\BOOKS\Python\python\python\$i E:\BOOKS\Python\python\python\$i.pdf }
Hi, I tried to rename the file under a folder using above command but not able to do and got below error.
Rename-Item : Cannot rename because item at 'E:\BOOKS\Python\python\python\book_pdf' does not exist.
At line:1 char:37
+ foreach( $i in $items) { Rename-Item <<<< E:\BOOKS\Python\python\python\$i E:\BOOKS\Python\python\python\$i.pdf }
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Looks like you want to change all '_pdf' to '.pdf', if so this is a pretty easy way to do it.
ls -Path 'E:\BOOKS\Python\python\python' -Filter *_pdf |
ForEach-Object {$_ | Rename-Item -NewName $_.Name.Replace('_pdf', '.pdf')}
You're over complicating things. Don't re-type out the path name to the file, use the FullName property already provided by the Get-ChildItem cmdlet. Then just use a substring of the BaseName property to remove the last 4 characters, and add ".pdf" to the end.
$folderpath = 'E:\BOOKS\Python\python\python'
$items = Get-ChildItem -Recurse $folderpathc *_pdf
foreach( $i in $items) {
Rename-Item $i.FullName ($i.basename.substring(0,$i.BaseName.length-4)+".pdf")
}
I have been working on a PowerShell script for the better part of well a week or two. I've been able to get some parts of it working however I'm unable to fully get this automated.
I deal with a lot of CSV files on a daily basis, I have been tasked with uploading them into our software however sometimes they're too large to handle so I break them down based upon their "type" (it's a column in the CSV) and I export it to a single CSV per "type". I've been able to accomplish this with the following:
$file = gci -Filter "*.csv";
Import-Csv $file `
| Group-Object –Property “type” `
| Foreach-Object `
{
$path=$_.name+”.csv” ; $_.group `
| Export-Csv –Path $path –NoTypeInformation
}
So this works wonderfully, for each individual CSV. Unfortunately I don't have the time to do this for each individual CSV. Now I come to my other PowerShell script:
get-childitem -Filter "*.csv" `
| select-object basename `
| foreach-object{ $path=$_.basename+".csv" #iterate through files.
if(!(Test-Path -path $_.basename)) #If the folder of the file can't be found then it will attempt to create it.
{
New-Item $_.basename -type directory; $file=$_.basename+".csv";
Import-Csv $file `
| Group-Object -Property "Type" `
| Foreach-Object {
$path=$_.name+".csv"; $_.group `
| `
if(!(Test-Path -path $path2))
{
New-Item $path2 -type directory
Export-Csv -Path $path2 + "\" + $path -NoTypeInformation
}
else
{
"Failed on: " + $_.basename
#Export-Csv -Path $_.basename + "\" + $path -NoTypeInformation
}
}
}
else
{
Import-Csv $path `
| Group-Object -Property "Type" `
| Foreach-Object {$path=$_.basename+".csv" ; $_.group
if(Test-Path -path $._)
{
New-Item $path2 -type directory
Export-Csv -Path $path2 + "\" + $path -NoTypeInformation
}
#else
#{
Write-Host "Failed on: $_.basename"
#Export-Csv -Path $_.basename + "\" + $path -NoTypeInformation
#}
}
}
}
I just can't wrap my head around "why" this isn't working effectively. I have two conditionals. Is there a folder for the CSV? If no create one. I have to have another one because one of the "types" contains a \ which errors out if I don't have the folder, so I automatically try to create it. When I run the script I get the Path is null.
The Error is:
The term ' ' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At C:\Users\c.burkinshaw\foldermake.ps1:11 char:26
+ | ` <<<<
+ CategoryInfo : ObjectNotFound: ( :String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Test-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\c.burkinshaw\foldermake.ps1:12 char:45
+ if(!(Test-Path -path <<<< $path2))
+ CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand
Any help would be greatly appreciated, if you have questions please don't hesitate to ask.
You have not defined $path2 anywhere, so something like test-path -path $path2 will say path is null. And in one place you are using $._ which will again give errors.
Edit after question updated with error message:
Your error message also says the same
Test-Path : Cannot bind argument to parameter 'Path' because it is
null. At C:\Users\c.burkinshaw\foldermake.ps1:12 char:45
+ if(!(Test-Path -path <<<< $path2))
Also the other error is in:
$path=$_.name+".csv"; $_.group `
| `
what are you trying to do here with the $_.group?
It is not proper. You cannot do $_.group | and provide some if statement.
Other comments:
Why are using $_.basename and then appending .csv? You could have just used $_.name. Try to not use the select-object basename - I don't see the value.
Extract the common import-csv and export-csv part into a function.