The specified network name is no longer available in powershell - powershell

I have list of share path in a text file. I try to read the files and folders in each path and exporting to csv file using powershell script. I got some csv files with 0 KB.
so i try to test the existance of such network path using Test-Path command. few path shows it is exist but when itry to list out the directories of existing path using Dir \sharepath name i got error like "The specified network name is no longer available" Why??
Sharing code below..
foreach ($dir in (Get-Content $infile)) {
$outfilecsv='jerin-Download'+'.csv'
Get-ChildItem -Path $dir -Filter *.* -Recurse | Select-Object
Name,#{Name="Owner";Expression={(Get-ACL $_.fullname).Owner}},CreationTime,#{Name="FileModifiedDate";Expression={$_.LastWriteTime}},
#{Name="FileAccessedDate";Expression={$_.LastAccessTime}},#{Name="Attributes";Expression=
{$_.Attributes}},#{l='ParentPath';e={Split-Path $_.FullName}},
#{Name="DormantFor(days)";Expression={[int]((Get-Date)-$_.LastWriteTime).TotalDays}},
#{N="FileCategory";E={Get-FileSizeCategory($_)}},
#{Name="Size";Expression={if($_.PSIsContainer -eq $True){(New-Object -com
Scripting.FileSystemObject).GetFolder( $_.FullName).Size} else {$_.Length}}}|
Export-Csv -Path $outfilecsv -Encoding ascii -NoTypeInformation
}
Can anyone suggest
Thanks
Jerin

Related

Powershell List Excel Files and Copy

I apologize for the naivety of this post, please forgive my newness.
I have approximately 20,000 network files to filter through and copy certain ones to a local drive.
File List Requirements:
Excel files of various type (.xls, .xlsx, .xlsm)
Only files modified after 4/1/2022
Only files that contain "2022" in the filename
If the file meets those requirements then:
Copy the file to a local folder (original folder path structure doesn't matter, all files can go in one folder)
Output the original path and filename to a txt file, along with the lastwritedate
I have created the following code, which successfully obtains all excel files and creates the filename list
Get-ChildItem "D:\network_folder\" -Filter *.xls -Recurse | Select-Object -Property FullName, LastWriteTime |
Export-Csv -Path "C:\local_folder\file_list.csv" -Force -NoTypeInformation
However I cannot figure out the following issues:
how and where to filter for the lastwritetime
how and where to filter for the "2022" in the name
how and where to copy the files to the local folder
right now I'm just putting this all in the command line, do I need to make some file to run this process?
Thank you for any assistance you can provide!
I guess you want something like this.
It searches for files in the source folder with 2022 in the name and having .xls (or anything following xls) as extension.
It then loops over these items, creates the subfolder structure where they were found in the destination folder, copies the files and finally writes out a CSV file with information of the original file.
$sourcePath = 'D:\network_folder'
$destination = 'D:\dest_folder'
$refDate = [datetime]::new(2022,4,2) # --> next day date as of midnight
Get-ChildItem -Path $sourcePath -Filter '*2022*.xls*' -File -Recurse |
Where-Object {$_.LastWriteTime -ge $refDate} | ForEach-Object {
# create the destination folder if it does not already exist
$target = Join-Path -Path $destination -ChildPath $_.DirectoryName.Substring($sourcePath.Length)
$null = New-Item -Path $target -ItemType Directory -Force
# copy the file
$_ | Copy-Item -Destination $target
# output the wanted properties from the original file
$_ | Select-Object Name, FullName, LastWriteTime
} | Export-Csv -Path "C:\local_folder\file_list.csv" -Force -NoTypeInformation

Copying files to specific folder declared in a CSV file using Powershell Script

i am quite new to powershell and i am trying to make a script that copy files to certain folders that are declared in a CSV file. But till now i am getting errors from everywhere and can't find nothing to resolve this issue.
I have this folders and .txt files created in the same folder as the script.
Till now i could only do this:
$files = Import-Csv .\files.csv
$files
foreach ($file in $files) {
$name = $file.name
$final = $file.destination
Copy-Item $name -Destination $final
}
This is my CSV
name;destination
file1.txt;folderX
file2.txt;folderY
file3.txt;folderZ
As the comments indicate, if you are not using default system delimiters, you should make sure to specify them.
I also recommend typically to use quotes for your csv to ensure no problems with accidentally including an entry that includes the delimiter in the name.
#"
"taco1.txt";"C:\temp\taco2;.txt"
"# | ConvertFrom-CSV -Delimiter ';' -Header #('file','destination')
will output
file destination
---- -----------
taco1.txt C:\temp\taco2;.txt
The quotes make sure the values are correctly interpreted. And yes... you can name a file foobar;test..txt. Never underestimate what users might do. 😁
If you take the command Get-ChildItem | Select-Object BaseName,Directory | ConvertTo-CSV -NoTypeInformation and review the output, you should see it quoted like this.
Sourcing Your File List
One last tip. Most of the time I've come across a CSV for file input lists a CSV hasn't been needed. Consider looking at grabbing the files you in your script itself.
For example, if you have a folder and need to filter the list down, you can do this on the fly very easily in PowerShell by using Get-ChildItem.
For example:
$Directory = 'C:\temp'
$Destination = $ENV:TEMP
Get-ChildItem -Path $Directory -Filter *.txt -Recurse | Copy-Item -Destination $Destination
If you need to have more granular matching control, consider using the Where-Object cmdlet and doing something like this:
Get-ChildItem -Path $Directory -Filter *.txt -Recurse | Where-Object Name -match '(taco)|(burrito)' | Copy-Item -Destination $Destination
Often you'll find that you can easily use this type of filtering to keep CSV and input files out of the solution.
example
Using techniques like this, you might be able to get files from 2 directories, filter the match, and copy all in a short statement like this:
Get-ChildItem -Path 'C:\temp' -Filter '*.xlsx' -Recurse | Where-Object Name -match 'taco' | Copy-Item -Destination $ENV:TEMP -Verbose
Hope that gives you some other ideas! Welcome to Stack Overflow. πŸ‘‹

Powershell script to list files without folder names

I open a PS in a folder then use
dir -name > asd.xls -recurse.
How can I modify this so it doesn't incude folders in the filenames?
Instead of using -name, try using
(Get-ChildItem -Recurse).Name > asd.xls
and be aware that you won’t get a valid Excel workbook that way. You can get a valid CSV that can be loaded into Excel with
(Get-ChildItem -Recurse) | Select-Object -Property Name | Export-CSV -Path asd.csv -NoTypeInformation

Search computer for .docx, .xls files using PowerShell

I am new to PowerShell and having difficulties trying to locate certain types of files (.doc, .docx, .xls, .xlsx), output the filenames and sizes (in groups by file extension) to a text file, and also include the total number files and total files size for each file extension.
The code that I have so far is:
$Report_File_Destination = "C:\Users\StayPositibve\Desktop\testing20.txt"
$path = ".\*"
Get-ChildItem $path -Include *.doc, *.docx, *.xls, *.xlsx -Recurse | Group-Object Extension -NoElement | Out-File $Report_File_Destination -Append
Every time I run this code, I receive a Get-ChildItem Access is Denied (I am running PowerShell as Administrator). What am I doing wrong? Thanks for your help!
This is due to the fact that it exists some paths that your are not allowed to browse. You can try to use -ErrorAction Ignore -Force options of Get-ChildItem to ignore these errors or force access to files that cannot otherwise be accessed by the user, such as hidden or system files. in old version of PowerShell you can test -ErrorAction SilentlyContinue.
Get-ChildItem $path -Include *.doc, *.docx, *.xls, *.xlsx -Recurse -ErrorAction Ignore -Force

Copy data for each user using PowerShell

I have a text file with a list of user names separated by semi colon, users names in the text file are: user1; user2; user3, etc.. The user names all have a network folder located at \testserver\users\user1, \testserver\users\user2, and so on.
I am trying to have PowerShell script read the text file and copy the folder and all data in each folder for each user from one location to another location called \testserver\newusers\users. However when I launch the script I have written so far, it just creates a folder with a user name from the text file I have. Below is what I have so far:
$File = Get-Content .\MyFile.txt
$File | ForEach-Object {
$_.Split(';') | ForEach-Object {
Copy-Item -Path "$_" -Destination '\\testserver\newusers\users'
}
}
I am launching my PowerShell .ps1 file from a location that has the myfile.txt file in it.
How do I get this to work properly?
Call Copy-Item with the parameter -Recurse if you want to copy the folders' content as well. Otherwise just the folder itself would be copied (without content). You also need to provide the full path to the source folders unless you run the script from \\testserver\users.
Something like this should work:
$server = 'testserver'
$src = "\\$server\users"
$dst = "\\$server\newusers"
(Get-Content .\MyFile.txt) -split ';' | % {
Copy-Item -Path "$src\$_" -Destination "$dst\" -Recurse
}