In the code Below I want to achieve both 'For' and 'ForEach' which will create folders in "C:\Sample Files for ForEach Loop" directory and sample text files in each
After running the code an error message Pops up stating : *At C:\Users\Nick\For Each scripting Construct.ps1:14 char:10
- for($folders in $directs){
Unexpected token 'in' in expression or statement.
- CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
- FullyQualifiedErrorId : UnexpectedToken*
#Address of folder
$directs = @("C:\Sample Files for ForEach Loop")
# 1. Creating an array of Folders using For Loop
for($folders in $directs){
$folders = New-Item -Path "C:\Sample Files for ForEach Loop" -ItemType Directory
}
#2. Using For Each Loop to create sample Textfiles inside them
foreach($sample in $folders) {
Add-Content -Path "$sample\SampleFile.txt" -Value "Hello World 1"
Add-Content -Path "$sample\SampleFile1.txt" -Value "Hello World 2"
Write-Host "$sample done."
}
Please guide me