The simplest way to get # cat
and # dog
in the example is using -Pattern '^# '
Though, in this case it must be ensured that the line always starts with # followed by at least one whitespace. Lines with leading whitespaces and also strings directly adjacent after # without any whitespaces between will not match.
# cat
######## foo
### bar
#################### fish
# dog
##### test
# bird
#monkey
For getting # cat
, # dog
, # bird
and #monkey
it's better to use:
Get-Content file.txt | Select-String -Pattern '^(\s*)#(?!#)'
The solution of using a negative lookahead (?!#)
has already been mentioned, don't know why the answer was downvoted.
^(\s*)
describes that at the start of the line any whitespace characters in zero or more occurrence before # will match.