Home > Mobile >  Powershell Script to create the file
Powershell Script to create the file

Time:10-25

I am new in Powershell. I tried to create the "dummy file" which is mentioned in the file.

For example, in the attached sheet, I have a file name called "Sheet" and there are total 5 files with different formats like - file1.pdf, file2.txt, file3.PNG, file4.docx, and file5.xlsx

I want to create the dummy file according to the file format.

So far I tried this code but it is not working. Please help me with it.

Thanks in advance

Screenshot: enter image description here

So far I tried this code but not working -

$file_list = Get-Content -Path "C:\Users\JamesAlam\Downloads\Sheet.txt"
$destination_folder = "C:\Users\JamesAlam\Downloads\Example"
$null = New-Item -Path $destination_folder -ItemType Directory -Force
foreach ($file in $file_list) {
     Copy-Item -Path $file -Destination $destination_folder -Recurse
}

CodePudding user response:

You should usethe New-Item cmdlet to create the new files. Also, you can simplify your script. If you add -Force to the New-Item cmdlet, it will also create the directory structure:

$destination_folder = "C:\Users\JamesAlam\Downloads\Example"

Get-Content -Path "C:\Users\JamesAlam\Downloads\Sheet.txt" | ForEach-Object {
    New-Item -Path (Join-Path $destination_folder $_) -Force
}
  • Related