Skip to content

Instantly share code, notes, and snippets.

@thecliguy
Last active August 15, 2018 13:23
Show Gist options
  • Save thecliguy/870b90b41995b4f95cf21c810f42f4af to your computer and use it in GitHub Desktop.
Save thecliguy/870b90b41995b4f95cf21c810f42f4af to your computer and use it in GitHub Desktop.
ForEach Output Experiments
# foreach pipeline without format-table
$col = 1,2,3
$col | foreach {
write-host "Doing thing $($_)..."
get-childitem C:\windows\regedit.exe
}
# Output:
# Doing thing 1...
#
#
# Directory: C:\windows
#
#
# Mode LastWriteTime Length Name
# ---- ------------- ------ ----
# -a---- 29/09/2017 14:41 335872 regedit.exe
# Doing thing 2...
# -a---- 29/09/2017 14:41 335872 regedit.exe
# Doing thing 3...
# -a---- 29/09/2017 14:41 335872 regedit.exe
################################################################################
# foreach pipeline with format-table
$col = 1,2,3
$col | foreach {
write-host "Doing thing $($_)..."
get-childitem C:\windows\regedit.exe | format-table
}
# Output:
# Doing thing 1...
#
#
# Directory: C:\windows
#
#
# Mode LastWriteTime Length Name
# ---- ------------- ------ ----
# -a---- 29/09/2017 14:41 335872 regedit.exe
#
#
# Doing thing 2...
#
#
# Directory: C:\windows
#
#
# Mode LastWriteTime Length Name
# ---- ------------- ------ ----
# -a---- 29/09/2017 14:41 335872 regedit.exe
#
#
# Doing thing 3...
#
#
# Directory: C:\windows
#
#
# Mode LastWriteTime Length Name
# ---- ------------- ------ ----
# -a---- 29/09/2017 14:41 335872 regedit.exe
################################################################################
# foreach method of a collection without format-table
$col = 1,2,3
$col.foreach({
write-host "Doing thing $($_)..."
get-childitem C:\windows\regedit.exe
})
# Doing thing 1...
# Doing thing 2...
# Doing thing 3...
#
#
# Directory: C:\windows
#
#
# Mode LastWriteTime Length Name
# ---- ------------- ------ ----
# -a---- 29/09/2017 14:41 335872 regedit.exe
# -a---- 29/09/2017 14:41 335872 regedit.exe
# -a---- 29/09/2017 14:41 335872 regedit.exe
################################################################################
# foreach method of a collection with format-table
$col = 1,2,3
$col.foreach({
write-host "Doing thing $($_)..."
get-childitem C:\windows\regedit.exe | format-table
})
# Doing thing 1...
# Doing thing 2...
# Doing thing 3...
#
#
# Directory: C:\windows
#
#
# Mode LastWriteTime Length Name
# ---- ------------- ------ ----
# -a---- 29/09/2017 14:41 335872 regedit.exe
#
#
#
#
# Directory: C:\windows
#
#
# Mode LastWriteTime Length Name
# ---- ------------- ------ ----
# -a---- 29/09/2017 14:41 335872 regedit.exe
#
#
#
#
# Directory: C:\windows
#
#
# Mode LastWriteTime Length Name
# ---- ------------- ------ ----
# -a---- 29/09/2017 14:41 335872 regedit.exe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment