Top 10 PowerShell Commands for Beginners | Realistic Examples with Explanations!

00:19:55
https://www.youtube.com/watch?v=MP_UR5iWfZQ

概要

TLDRIn this video, Josh introduces viewers to ten beginner-friendly PowerShell commands that are essential for anyone in IT or exploring cybersecurity. PowerShell is a scripting language native to Windows, but it can also be installed on macOS and Linux, making it a valuable tool for automation tasks across different platforms. Josh explains how he has applied these commands in various professional roles, emphasizing their practicality and utility. The video covers how to read file content with 'Get-Content', use 'ForEach-Object' for iteration, output data to files with 'Out-File', and check network connections with 'Test-NetConnection'. Further covered are data conversion using 'ConvertTo-Json', obtaining date information with 'Get-Date', utilizing 'Start-Sleep' to pause scripts, and using 'Write-Host' for outputting text. The video concludes with 'Get-Command' for identifying commands and 'Get-Help' for assistance and examples, encouraging viewers to experiment without memorizing everything immediately. Josh also offers additional resources and mentoring support for further learning.

収穫

  • 🔍 PowerShell can automate almost any task on a computer.
  • 💻 PowerShell is used in IT and cybersecurity jobs.
  • 📜 'Get-Content' reads file content for automation tasks.
  • 🔁 'ForEach-Object' helps iterate over collections in scripts.
  • 📁 'Out-File' creates text file outputs for logs.
  • 🌐 'Test-NetConnection' checks network connections, like ping.
  • 🔄 'ConvertTo-Json' shows object structures in JSON.
  • 📅 'Get-Date' fetches current date/time for scripts.
  • ⏸️ 'Start-Sleep' pauses scripts to manage command execution.
  • 📚 'Get-Command' and 'Get-Help' provide command information and examples.

タイムライン

  • 00:00:00 - 00:05:00

    Josh introduces his video on the top 10 beginner-friendly PowerShell commands, emphasizing PowerShell's utility across IT and cybersecurity jobs for automation. He describes PowerShell as a versatile scripting language, initially for Windows but also available on macOS and Linux. The video aims to introduce viewers to PowerShell, offering entertainment or practical learning, with a promise that viewers will gain valuable insights. Josh begins by showing how to open PowerShell ISE and set up a workspace with Windows Explorer. He discusses the first command, 'Get-Content', which reads text file content, and illustrates its utility by creating a text file to demonstrate the command's function. He explains how to store content into variables for manipulation.

  • 00:05:00 - 00:10:00

    Josh explains the 'ForEach-Object' command, comparing it to a 'for each' loop in other programming languages, and shows how to iterate through file content to perform actions on each item. He demonstrates manipulating file content by printing each line with custom formatting. Josh stresses that memorizing commands isn't necessary; understanding their potential is key, as one can always search online for exact syntax later. He introduces 'Out-File', a command for outputting PowerShell data to text files, demonstrating its utility in writing logs. He extends previous examples by showing how file content outputs can be redirected to a text file, emphasizing logging applications. Next, he covers 'Test-NetConnection', likened to a more advanced 'ping' command. Josh showcases its use in testing network connections, suggesting its value in network diagnostics, including port checking and script integration for multiple host checks.

  • 00:10:00 - 00:19:55

    Josh discusses the 'ConvertTo-Json' command, useful for viewing object structures in PowerShell, which he frequently uses to interact with complex data objects. He moves on to 'Get-Date', highlighting its ability to fetch and format dates, useful in script logging and time-related functions. Josh introduces 'Start-Sleep' for pausing script execution, necessary for handling processes requiring timed intervals, such as rate-limited APIs. He briefly mentions the 'Write-Host' command for console output, noting its customization capabilities for improved script readability and feedback. The last two commands, 'Get-Command' and 'Get-Help', are emphasized as critical. 'Get-Command' aids in discovering available commands, while 'Get-Help' offers detailed command usage instructions, with an online option for extended documentation. Josh concludes by promoting his channel, encouraging engagement, and offering additional resources like mentoring and Patreon for viewer support.

マインドマップ

Mind Map

よくある質問

  • What is PowerShell?

    PowerShell is a programming and scripting language originally native to Windows, but also available on macOS and Linux.

  • Why is it important to learn PowerShell?

    PowerShell is a versatile tool for automating tasks on computers, useful in IT and cybersecurity professions.

  • What does the 'Get-Content' command do?

    It reads the content of a text file and can be used to perform actions on each item within the file.

  • What is the 'ForEach-Object' command used for?

    It allows iteration through a collection of objects, similar to a 'for each' loop in other programming languages.

  • How does the 'Out-File' command work?

    It outputs data from PowerShell to a text file, useful for logging and record keeping.

  • What can 'Test-NetConnection' be used for?

    It tests network connections and ports, acting like a more detailed ping command.

  • What is the purpose of 'ConvertTo-Json'?

    It converts PowerShell objects to JSON format, helpful for viewing object structures.

  • What is 'Get-Date' useful for?

    It retrieves the current date and time, and can be used in scripts that require date manipulation.

  • How is 'Start-Sleep' beneficial?

    It pauses the execution of a script, useful to control timing between command execution.

  • What does 'Get-Help' do?

    It provides detailed help and examples for PowerShell commands, and can connect to online resources for more information.

ビデオをもっと見る

AIを活用したYouTubeの無料動画要約に即アクセス!
字幕
en
オートスクロール:
  • 00:00:00
    hey everybody josh here again welcome
  • 00:00:01
    back to my channel in today's video
  • 00:00:02
    we're going to be covering my top 10
  • 00:00:04
    beginner-friendly powershell commands
  • 00:00:06
    powershell is essentially a programming
  • 00:00:09
    scripting language that's originally
  • 00:00:10
    native to windows but you can install it
  • 00:00:12
    on mac os and and linux as well pretty
  • 00:00:15
    much like anything that you can do on
  • 00:00:16
    the computer anything that you can
  • 00:00:17
    imagine you can automate it in
  • 00:00:19
    powershell in one way or another i've
  • 00:00:21
    used powershell for like every single
  • 00:00:22
    one of my it jobs and every single one
  • 00:00:24
    of my cyber security jobs and i even had
  • 00:00:26
    a job where i like specifically like
  • 00:00:28
    ninety percent of my time i was
  • 00:00:30
    developing some big automation project
  • 00:00:32
    uh using like 100 powershell so it's
  • 00:00:35
    really useful to know at the very least
  • 00:00:36
    if you're interested in it or cyber
  • 00:00:38
    security i think this video will
  • 00:00:39
    probably be interesting to you at the
  • 00:00:41
    most it will be like really helpful to
  • 00:00:43
    you kind of opening your brain up to the
  • 00:00:45
    powershell world so let's go ahead and
  • 00:00:47
    get started so feel free to follow along
  • 00:00:50
    if you want with your own like kind of
  • 00:00:52
    environment or you can just watch and be
  • 00:00:53
    entertained as well you'll definitely
  • 00:00:55
    get something out of it either way so
  • 00:00:56
    okay so first thing i'll do i'll just
  • 00:00:58
    open up powershell ice and i'll just
  • 00:01:00
    kind of open it up on one side of the
  • 00:01:01
    screen and on the other side of the
  • 00:01:03
    screen i'll open up a windows explorer
  • 00:01:06
    and i'll just go to the c drive and then
  • 00:01:08
    i'm just gonna make a folder here
  • 00:01:09
    because we're gonna work with files and
  • 00:01:11
    stuff a little bit so i'll just make a
  • 00:01:12
    folder called powershell demo and i'll
  • 00:01:14
    go to it see it's empty and then on the
  • 00:01:16
    powershell command line down here i'll
  • 00:01:18
    just change directory to the powershell
  • 00:01:20
    demo folder like this just so i can kind
  • 00:01:22
    of have it here okay so for the very
  • 00:01:24
    first command it's going to be get
  • 00:01:26
    content get content and basically what
  • 00:01:28
    this does is it reads all the content
  • 00:01:31
    from a given text file for example so in
  • 00:01:33
    our folder over here i'll just go ahead
  • 00:01:35
    and make like a text file like super
  • 00:01:37
    quickly i'll just call it hosts and then
  • 00:01:39
    inside i'll put like google.com
  • 00:01:42
    instagram.com
  • 00:01:44
    facebook.com and i'll just save it and
  • 00:01:46
    then i'll say get content here and then
  • 00:01:48
    i'll just say host dot text and i will
  • 00:01:51
    press play and then you can kind of see
  • 00:01:53
    simply here it just prints these out to
  • 00:01:55
    the screen there's a lot of different
  • 00:01:57
    utility for these like if you want to do
  • 00:01:59
    some action to each one of the items in
  • 00:02:00
    the list you can you can do that uh you
  • 00:02:02
    can also this is how you make a variable
  • 00:02:04
    in powershell you do like a dollar sign
  • 00:02:06
    and then you you name it something like
  • 00:02:07
    this like so and then if you say play
  • 00:02:09
    then the script runs and you notice
  • 00:02:11
    nothing got printed out but actually the
  • 00:02:13
    content of hosts got stored into this
  • 00:02:16
    variable so for example if i go down
  • 00:02:17
    here and type posts and say enter it
  • 00:02:19
    will print out the content of this
  • 00:02:22
    host's file over here so i use this like
  • 00:02:24
    all the time because there's a lot of
  • 00:02:26
    instances where you have to like read in
  • 00:02:27
    some text from a file and kind of do
  • 00:02:29
    something with it and print it to the
  • 00:02:30
    screen and then the second command that
  • 00:02:32
    i use all the time is the for each
  • 00:02:34
    object command if you're if you've done
  • 00:02:36
    like other programming before you can
  • 00:02:38
    kind of think about it as a for each
  • 00:02:39
    loop because that's essentially exactly
  • 00:02:41
    what it is but basically the for each
  • 00:02:43
    command lets you iterate through a
  • 00:02:45
    collection of objects so what i mean by
  • 00:02:47
    that is when we do like this get content
  • 00:02:50
    and bring in like the hosts like bring
  • 00:02:52
    in these three things and say we want to
  • 00:02:54
    like store it to a variable we can do we
  • 00:02:56
    can then do this essentially
  • 00:02:58
    for each object and then we can do like
  • 00:03:01
    open close braces to indicate like the
  • 00:03:03
    block of code where we do stuff in and
  • 00:03:06
    then when we say like dollar sign
  • 00:03:08
    underscore this this represents
  • 00:03:10
    essentially this dollar sign underscore
  • 00:03:12
    represents each one of these it's kind
  • 00:03:14
    of confusing at first but maybe try to
  • 00:03:16
    recognize what happens when we actually
  • 00:03:17
    run the code so i'll run this and it
  • 00:03:19
    pretty pretty much looks the same it
  • 00:03:21
    just prints them out one at a time but
  • 00:03:22
    the kind of difference with this is it
  • 00:03:24
    you have the chance to to do something
  • 00:03:26
    to these items before they actually get
  • 00:03:28
    printed to the screen so for example we
  • 00:03:30
    can do something like
  • 00:03:33
    something like this and then run this
  • 00:03:35
    and run this and oh you can't really see
  • 00:03:38
    it very good
  • 00:03:40
    yeah so background color white
  • 00:03:41
    foreground color black it lets us
  • 00:03:43
    manipulate this this these objects in
  • 00:03:45
    one way or another like each time the
  • 00:03:47
    loop iterates it presents us with like
  • 00:03:50
    for example the first time the loop
  • 00:03:51
    iterates this dollar sign underscore
  • 00:03:53
    presents us with this the second time
  • 00:03:54
    this the third time this and then so
  • 00:03:56
    forth if there's more items in your the
  • 00:03:59
    content that you got from here if that
  • 00:04:00
    makes sense so for each object kind of
  • 00:04:02
    just lets you do something with each
  • 00:04:04
    object in the list of things so we can
  • 00:04:06
    do other things too like we can say for
  • 00:04:08
    example
  • 00:04:10
    like this
  • 00:04:12
    you can say hostname we did hostname
  • 00:04:14
    colon and then the actual thing that's
  • 00:04:16
    on that line if that makes sense so it's
  • 00:04:18
    essentially a for each loop i use this
  • 00:04:20
    all the time and production scripts and
  • 00:04:22
    stuff i do my personal projects it's
  • 00:04:24
    just like something you use all the time
  • 00:04:26
    and by the way like as you're watching
  • 00:04:27
    this don't look at this and be like oh
  • 00:04:29
    man i have to memorize all these
  • 00:04:30
    commands i have to memorize these like
  • 00:04:32
    you don't have to memorize anything just
  • 00:04:33
    look at it and kind of acknowledge that
  • 00:04:35
    it's possible to do and kind of just do
  • 00:04:37
    your best to understand what's going on
  • 00:04:38
    and then if you need to remember again
  • 00:04:40
    in the future you can always just google
  • 00:04:42
    like oh how to how to do for each
  • 00:04:44
    powershell like oh how to like read in
  • 00:04:46
    text file powershell and then it will
  • 00:04:48
    come back to you at that time so yeah
  • 00:04:49
    and then the third command that i use
  • 00:04:51
    like all the time is out file and
  • 00:04:53
    basically what outfile does is it
  • 00:04:55
    essentially like takes something that's
  • 00:04:57
    in powershell and then just outputs it
  • 00:04:59
    to a text file so for example i'll just
  • 00:05:02
    actually i'll leave this here for now so
  • 00:05:04
    you can say
  • 00:05:05
    something really simple my name is josh
  • 00:05:08
    and then do a pipe out file and then you
  • 00:05:11
    can press tab and then you can pick like
  • 00:05:13
    the file path or i believe you can just
  • 00:05:15
    if you want it to be in the same
  • 00:05:16
    directory you can just start typing the
  • 00:05:18
    name of the file s dot text and then if
  • 00:05:21
    you say append it will add on to the
  • 00:05:23
    file if there's something already there
  • 00:05:24
    so i'll say append
  • 00:05:26
    and that's that's good enough so i'll
  • 00:05:27
    just copy this and i'll do one more line
  • 00:05:30
    this and i'll save it and then you can
  • 00:05:31
    just highlight these two lines like this
  • 00:05:33
    and you can click this play button and
  • 00:05:35
    it will only run these two so i'll
  • 00:05:37
    display these i'll close this and then
  • 00:05:39
    we can see that there's a test.txt file
  • 00:05:42
    in here so if i open this up you can
  • 00:05:43
    kind of see like what happened here like
  • 00:05:45
    my name is josh my name my name is timmy
  • 00:05:47
    i use this all the time it's really good
  • 00:05:48
    for like writing vlogs if you've ever
  • 00:05:50
    opened up like a log file there's like a
  • 00:05:52
    date and like something that happened
  • 00:05:54
    that it's really good for that kind of
  • 00:05:55
    thing so we can do something like here
  • 00:05:57
    we can go back to our our hosts i'll
  • 00:05:58
    just like close this for now we can go
  • 00:06:00
    back to our kind of hosts thing that we
  • 00:06:03
    did up here and instead of write host we
  • 00:06:05
    i'll just do something like
  • 00:06:06
    yeah i'll just do this so normally if we
  • 00:06:09
    run this right it just prints these
  • 00:06:10
    three to the console but i could do
  • 00:06:12
    something like this
  • 00:06:15
    out put hosts text now i'll append it
  • 00:06:19
    i'll save this and i'll run this and
  • 00:06:21
    then we have like another text file over
  • 00:06:22
    here called output hosts and then it
  • 00:06:25
    kind of matches what we previously had
  • 00:06:27
    printing printing to the console we just
  • 00:06:28
    like redirected it to this text file
  • 00:06:31
    here again i use this all the time for
  • 00:06:33
    like writing logs and and just if i need
  • 00:06:35
    to write something to a text file for
  • 00:06:36
    whatever reason use it all the time so
  • 00:06:38
    that is uh out file the outfile command
  • 00:06:41
    and the next command on the list is test
  • 00:06:43
    net connection and you can think about
  • 00:06:45
    this as kind of like a ping on steroids
  • 00:06:47
    essentially so i'll just i'll just type
  • 00:06:49
    it and i'll show you so test net this is
  • 00:06:51
    a great command by the way test net
  • 00:06:52
    connection and then like i don't
  • 00:06:54
    remember what the parameters are so you
  • 00:06:56
    can always press tab when you're working
  • 00:06:57
    in powershell ice or ise and then you
  • 00:06:59
    can kind of see the parameters so we can
  • 00:07:01
    say computer name and then we can just
  • 00:07:03
    enter like you know something like
  • 00:07:04
    google.com and then i only want to run
  • 00:07:06
    this line so i'll just click this thing
  • 00:07:08
    and then you can see it kind of returned
  • 00:07:10
    uh this kind of basic things like
  • 00:07:12
    computer name remote address this is ip6
  • 00:07:15
    address ethernet ping ping success and
  • 00:07:17
    then the the latency essentially so
  • 00:07:19
    there's a lot more inside of this
  • 00:07:21
    command but it kind of it shows you like
  • 00:07:23
    a really high level summary of the
  • 00:07:25
    status of the the net connection that
  • 00:07:26
    was tested there's a lot of ways that we
  • 00:07:28
    can see like the full detail of this so
  • 00:07:30
    we can we can do like i'll just assign
  • 00:07:32
    this to a variable so i'll say like host
  • 00:07:34
    status equals and then i'll run this
  • 00:07:36
    again and basically it's going to assign
  • 00:07:37
    the status of this command to this
  • 00:07:39
    variable so i'll run this and i can say
  • 00:07:42
    i can go down here even i can say host
  • 00:07:44
    status and then i can do a pipe and then
  • 00:07:47
    i i'll say like select everything and
  • 00:07:49
    we'll say enter and it shows like much
  • 00:07:52
    there's like much much more that you can
  • 00:07:54
    see a bunch of like dns records and and
  • 00:07:56
    some other kind of information in here
  • 00:07:58
    and it's kind of interesting uh going
  • 00:08:00
    back to this there's kind of more stuff
  • 00:08:01
    you can do with this you can check if
  • 00:08:03
    certain ports are open so for example um
  • 00:08:06
    port right here like i don't know if
  • 00:08:08
    you've studied like security plus or
  • 00:08:09
    network plus or anything but port 443 is
  • 00:08:12
    usually like https maybe they're using a
  • 00:08:14
    tls or something like this so we can
  • 00:08:16
    test if por 443 is open for google.com
  • 00:08:19
    so we'll click here and then we can say
  • 00:08:21
    tcp tests succeeded true when before it
  • 00:08:23
    said like a ping ping succeeded so now
  • 00:08:26
    we're doing we're like pinging but it's
  • 00:08:28
    over like tcp port 443 for instance so
  • 00:08:31
    we can kind of take this even like a bit
  • 00:08:33
    further and kind of integrate some of
  • 00:08:35
    our other commands that we've used so
  • 00:08:37
    instead of just doing like the
  • 00:08:38
    individual like you know testing of
  • 00:08:40
    google.com we can kind of erase this and
  • 00:08:43
    then we can get rid of the stuff and
  • 00:08:45
    then remember like this hosts thing
  • 00:08:46
    we're reading in like these hosts like
  • 00:08:49
    google.com instagram.com facebook so we
  • 00:08:51
    can kind of set this uh for each loop or
  • 00:08:54
    object to go through and like ping test
  • 00:08:56
    like each one of those if that makes
  • 00:08:57
    sense so remember if we just do like oh
  • 00:08:59
    not hosts if we just do like dollar sign
  • 00:09:02
    underscore and then run this it just
  • 00:09:03
    prints out google instagram and facebook
  • 00:09:06
    so if we want to do like a test net
  • 00:09:08
    connection for each one of them we can
  • 00:09:09
    do something like this we can do test
  • 00:09:12
    net connection computer name and this is
  • 00:09:15
    our computer name remember the dollar
  • 00:09:16
    sign underscore represents kind of each
  • 00:09:19
    one of these lines and then we can say
  • 00:09:21
    port 443 and we can say 480 and then we
  • 00:09:25
    can say like something like right host
  • 00:09:27
    testing host
  • 00:09:29
    and then dollar sign underscore and then
  • 00:09:31
    we'll just do like a an empty
  • 00:09:33
    and empty one afterwards so you can kind
  • 00:09:35
    of maybe guess what's going to happen
  • 00:09:36
    with this and then we'll run it and then
  • 00:09:38
    kind of observe what happens here so
  • 00:09:40
    that went really fast so basically we
  • 00:09:42
    have testing host google.com and then it
  • 00:09:44
    says like we see like testinghost
  • 00:09:46
    google.com and it kind of shows we
  • 00:09:48
    tested google over 443 and it was up and
  • 00:09:51
    over 80 and it was up testing hosts
  • 00:09:53
    instagram.com over 443 up 80 up and then
  • 00:09:56
    facebook 443 up and then 80 is is true
  • 00:09:59
    as well so it's pretty interesting like
  • 00:10:00
    what you can do you can kind of think
  • 00:10:02
    about how this might be useful if you
  • 00:10:04
    have like a bunch of hosts you want to
  • 00:10:05
    check to see if they're online or
  • 00:10:06
    something you have a big ass like text
  • 00:10:08
    file full of host names that's you know
  • 00:10:10
    something you know that's how you could
  • 00:10:11
    use these commands in conjunction with
  • 00:10:13
    each other to kind of do something
  • 00:10:15
    useful that has a business case business
  • 00:10:17
    use case and the next command on the
  • 00:10:18
    list is
  • 00:10:20
    convert to json so this command it's not
  • 00:10:22
    that useful unless you know what json is
  • 00:10:24
    um json's just
  • 00:10:26
    javascript object notation it's kind of
  • 00:10:28
    used to represent data in a certain way
  • 00:10:31
    and i i tend to use it a lot in
  • 00:10:33
    powershell because a lot of the items
  • 00:10:35
    and stuff you see in powershell they're
  • 00:10:37
    actually objects for example for example
  • 00:10:39
    we'll take the hosts variable that we
  • 00:10:41
    made up here inside of the host variable
  • 00:10:43
    if we say enter you can see it's simply
  • 00:10:45
    just like google.com instagram and
  • 00:10:47
    facebook.com but there's usually in
  • 00:10:49
    powershell objects there's usually much
  • 00:10:51
    more involved in that and if for some
  • 00:10:53
    reason i want to see like what's in the
  • 00:10:54
    object because i want to access it or
  • 00:10:56
    read it for for some reason or another i
  • 00:10:58
    usually use convert to json to kind of
  • 00:11:01
    see what the object looks like if that
  • 00:11:02
    makes sense so i'll just do a demo we'll
  • 00:11:05
    show you like what it looks like so just
  • 00:11:06
    normalhost.com has this inside it but it
  • 00:11:08
    can convert to json you can kind of see
  • 00:11:11
    it has like much much more properties in
  • 00:11:13
    it than you than you might have thought
  • 00:11:15
    so it kind of shows like the value so
  • 00:11:18
    it's essentially like a list of text it
  • 00:11:20
    kind of shows like all this like
  • 00:11:22
    information about it like ps path it's
  • 00:11:24
    like an array right and it has like
  • 00:11:26
    value of this ps path is this where like
  • 00:11:29
    where it lives the folder it lives and
  • 00:11:30
    is this the child name is this and like
  • 00:11:32
    all this kind of other stuff so it's
  • 00:11:33
    possible to to reference these things
  • 00:11:36
    inside of here like you don't have to
  • 00:11:37
    don't care about this too much but for
  • 00:11:39
    example hosts it's it looks like it's
  • 00:11:41
    actually an array so if i want to do
  • 00:11:43
    like the zeroth element of hosts i can
  • 00:11:45
    say enter and it actually like shows
  • 00:11:48
    google.com and like the first one it's
  • 00:11:50
    instagram.com it's kind of interesting
  • 00:11:52
    to like look at what certain objects are
  • 00:11:53
    and then you can see the structure of
  • 00:11:55
    them then you can like access data and
  • 00:11:57
    like manipulate them in in different
  • 00:11:58
    ways so for example if i do host 0 and
  • 00:12:01
    then i do convert to json i can see like
  • 00:12:04
    all this stuff it's it's really crazy
  • 00:12:06
    this might be overwhelming to look at so
  • 00:12:08
    don't don't like worry about it too much
  • 00:12:09
    and the next command that's on the list
  • 00:12:11
    is get date and that does pretty much
  • 00:12:13
    what you think it would do so get date
  • 00:12:16
    you can say enter and it kind of spits
  • 00:12:18
    out a date for you here get date and
  • 00:12:20
    then you can you can kind of uh get
  • 00:12:22
    specific parts of the date by putting it
  • 00:12:24
    in parentheses then you can say like
  • 00:12:25
    getdate.date or you can say getdate.day
  • 00:12:30
    day of week and it says thursday right
  • 00:12:32
    because today's thursday it's just kind
  • 00:12:33
    of a useful thing to do so for example
  • 00:12:35
    one way that this is useful is
  • 00:12:38
    like if we want to write some kind of
  • 00:12:40
    log log file or something like this so
  • 00:12:42
    again going back to our trusty for each
  • 00:12:46
    loop here we can add like a line in here
  • 00:12:48
    that says something like testing host
  • 00:12:51
    well we're going to make a an output to
  • 00:12:52
    a log file here with the data in it so
  • 00:12:54
    we'll say something like testing host
  • 00:12:56
    dollar sign underscore remember that
  • 00:12:58
    represents each one of these lines
  • 00:12:59
    dollar sign underscore dollar sign
  • 00:13:01
    underscore every time this thing
  • 00:13:02
    iterates we'll do this and then this is
  • 00:13:04
    going to be like a log file so we're
  • 00:13:06
    going to put like a date before the
  • 00:13:08
    actual message that we're outputting to
  • 00:13:10
    the log so we can do this get date yeah
  • 00:13:12
    we'll just do this get date and then
  • 00:13:13
    we'll do like a colon in a space and
  • 00:13:15
    then we're going to output this to we're
  • 00:13:18
    going to use out file again we'll say
  • 00:13:19
    out file uh we'll just name this host
  • 00:13:22
    test.log something like this and then
  • 00:13:24
    we'll append it so we'll run this it's
  • 00:13:26
    going to spit out a bunch of crap on the
  • 00:13:28
    screen down here and then it's also
  • 00:13:30
    going to give us a log file over here
  • 00:13:32
    based on this so i'll just run this
  • 00:13:34
    whole file let's say run here and the
  • 00:13:36
    same thing happened out here again and
  • 00:13:37
    then if we look on our host test.log it
  • 00:13:40
    pretty much just printed out this thing
  • 00:13:41
    right here this get date testing host
  • 00:13:43
    and then the host name so we have the
  • 00:13:45
    date the time testing the host and then
  • 00:13:47
    the host and then the host name i should
  • 00:13:48
    i should probably put like a hole in
  • 00:13:50
    here or something but you you kind of
  • 00:13:52
    get the point um get date you can use it
  • 00:13:53
    to get the date you can get like
  • 00:13:55
    specific specifically the year the day
  • 00:13:57
    the month like if you need to like
  • 00:13:59
    manipulate the date in a certain way i
  • 00:14:01
    always do that with scripts um because
  • 00:14:03
    sometimes you need the date in a certain
  • 00:14:04
    format but yeah good day always use it
  • 00:14:06
    like all the time and the next command
  • 00:14:08
    is a really easy one it's just called
  • 00:14:10
    start sleep all it does is essentially
  • 00:14:12
    pause the script for a certain amount of
  • 00:14:14
    time you kind of notice in this last log
  • 00:14:16
    that we ran here there's like not very
  • 00:14:18
    much time in between the log entries
  • 00:14:21
    which is okay but sometimes you want to
  • 00:14:22
    slow down your script a little bit
  • 00:14:24
    especially if you're dealing with like
  • 00:14:25
    apis you're dealing with like a slow
  • 00:14:27
    system or something you're processing
  • 00:14:29
    like a large number of records where you
  • 00:14:30
    can't just like dump all of them for one
  • 00:14:32
    reason reason or another so you can do
  • 00:14:35
    start sleep so this is our loop here so
  • 00:14:37
    if i just do like start sleep i can put
  • 00:14:39
    some time in between like each time the
  • 00:14:41
    actions take place in the loop if that
  • 00:14:43
    makes sense so i can say like start
  • 00:14:45
    sleep and then you can say like seconds
  • 00:14:47
    or milliseconds so we'll just do like
  • 00:14:48
    seconds and we'll say like two seconds
  • 00:14:51
    for example and i'll i'll just do
  • 00:14:52
    something interesting here i'll use
  • 00:14:54
    right hooks again and i'll say sleeping
  • 00:14:56
    or two seconds background color maybe
  • 00:14:59
    i'll do black foreground i'll do cyan
  • 00:15:02
    just so we can do something a little bit
  • 00:15:03
    more interesting so basically this
  • 00:15:05
    script is gonna run it's gonna output
  • 00:15:07
    this pause for two seconds it's gonna
  • 00:15:09
    ride out this testing host to the
  • 00:15:11
    command line it's going to log something
  • 00:15:13
    and then it's going to test 443 and 80
  • 00:15:15
    for these three hosts that are in here
  • 00:15:17
    so i'll get rid of i'll just do 443 so
  • 00:15:20
    i'll save this and i'll run it we see oh
  • 00:15:22
    god that's hard to read
  • 00:15:24
    i don't know i said it's weird so you
  • 00:15:26
    can see it's sleeping for two seconds
  • 00:15:27
    runs it sleeping for two seconds runs
  • 00:15:29
    it's sleeping for two seconds and then
  • 00:15:31
    kind of runs it again now if i look in
  • 00:15:32
    our uh host test.log you can kind of see
  • 00:15:35
    what's been happening a little bit you
  • 00:15:36
    can see there's like exactly two seconds
  • 00:15:38
    between these like tests for the most
  • 00:15:40
    part this is this log format is bad but
  • 00:15:42
    anyway you could get the idea
  • 00:15:44
    the idea behind it so yeah that is start
  • 00:15:47
    sleep and the next command i've been
  • 00:15:49
    using it like all along this whole time
  • 00:15:51
    it's just essentially right host right
  • 00:15:53
    host i use it all the time to like
  • 00:15:54
    output stuff to the screen it's really
  • 00:15:56
    useful because you can like make colors
  • 00:15:57
    and stuff like the background color you
  • 00:15:59
    can do you know whatever color you think
  • 00:16:02
    is cool foreground color red my name is
  • 00:16:05
    josh it's just very useful like this my
  • 00:16:07
    name is josh of course you can just like
  • 00:16:09
    forego this whole thing and just you can
  • 00:16:11
    just do this in powershell and it runs
  • 00:16:13
    still but if you want to do something
  • 00:16:15
    like add color to it or do something
  • 00:16:17
    like this you can you can use right host
  • 00:16:19
    and you can do you can kind of customize
  • 00:16:21
    it it's really useful use it all the
  • 00:16:22
    time of course you saw me like using it
  • 00:16:24
    already like in the very beginning but
  • 00:16:26
    right host and the last two commands
  • 00:16:27
    they're kind of they're kind of boring
  • 00:16:29
    but they're also like really really
  • 00:16:30
    really useful and if you remember like
  • 00:16:32
    if you want to memorize like any
  • 00:16:33
    commands in those lists just memorize
  • 00:16:35
    the last two so this command it's just
  • 00:16:37
    called git command it's it's very very
  • 00:16:40
    simple so you can say git command and if
  • 00:16:43
    you don't remember a command you can use
  • 00:16:45
    git command to kind of find what it is
  • 00:16:48
    so for example i'll use git command star
  • 00:16:50
    right star and then it shows all the
  • 00:16:52
    powershell commands with the word right
  • 00:16:55
    in it so you can see inside of here like
  • 00:16:56
    write host like if you couldn't remember
  • 00:16:58
    the right host command but if you just
  • 00:17:00
    remembered host or just remembered right
  • 00:17:02
    you could use like git command to find
  • 00:17:03
    it you can pretty much find like any any
  • 00:17:06
    command like this as long as you know
  • 00:17:08
    like what part of the command is so for
  • 00:17:10
    example we can do like git command if we
  • 00:17:12
    want to like figure out how to reference
  • 00:17:14
    the hard disk or something like that we
  • 00:17:16
    can do git command star disk star and
  • 00:17:19
    say enter and then you see like all
  • 00:17:20
    these different commands that you can do
  • 00:17:23
    like get disk get disk image whole bunch
  • 00:17:25
    of disk commands or maybe you're like oh
  • 00:17:27
    what does git disk do you can say like
  • 00:17:29
    get disk oh my god let me shrink this
  • 00:17:31
    and try it again and it looks like it
  • 00:17:32
    printed out a bunch of information about
  • 00:17:34
    my my hard disk it seems like we can do
  • 00:17:37
    git disk select star and then you can
  • 00:17:39
    really see a bunch of information about
  • 00:17:41
    my hard drive so i have an nvme drive
  • 00:17:44
    the size is probably on here somewhere i
  • 00:17:46
    did see it just like a second ago you're
  • 00:17:48
    probably looking at it right now oh it's
  • 00:17:50
    right here this i i think this is like
  • 00:17:51
    half a terabyte i think this is in bytes
  • 00:17:53
    but anyway you kind of uh get the idea
  • 00:17:56
    like git command maybe there's like a
  • 00:17:58
    command that has to do with the cpu i
  • 00:18:00
    don't know doesn't look like it how
  • 00:18:01
    about processor doesn't look like it
  • 00:18:03
    about network whole bunch of network
  • 00:18:05
    commands uh by the way you can get stuff
  • 00:18:07
    about like the cpu and processor and
  • 00:18:09
    powershell but those just apparently
  • 00:18:11
    weren't the right commands but anyway
  • 00:18:12
    git command is really good and that kind
  • 00:18:14
    of leads us into our last command so if
  • 00:18:17
    you remember like any command in
  • 00:18:19
    powershell just remember this one this
  • 00:18:21
    is the the get help command you can
  • 00:18:23
    always just type git help and then for
  • 00:18:25
    example test net connection and you can
  • 00:18:28
    say like online like this and oh i guess
  • 00:18:31
    like every command doesn't have an
  • 00:18:33
    online aspect to it so maybe we can do
  • 00:18:36
    like instead of test net connection
  • 00:18:37
    we'll do like get content and then it
  • 00:18:40
    kind of opens up a web page um straight
  • 00:18:42
    to the microsoft docs that kind of shows
  • 00:18:45
    you how to use this command and it has
  • 00:18:46
    like a bunch of examples in here of how
  • 00:18:49
    it works if that makes sense so i i use
  • 00:18:51
    this quite a bit like if i i know that i
  • 00:18:52
    want to do something i know it's
  • 00:18:54
    possible but i don't really know how
  • 00:18:55
    i'll use like get help dash on whatever
  • 00:18:58
    the command is like dash online and it
  • 00:19:00
    will i'll like look at examples and kind
  • 00:19:02
    of learn how to do it yeah if you found
  • 00:19:03
    that interesting i have like this other
  • 00:19:05
    video here that kind of goes through
  • 00:19:06
    like setting up active directory and
  • 00:19:08
    like setting up the infrastructure and
  • 00:19:10
    like kind of toward the end i go through
  • 00:19:12
    this like long script where we like
  • 00:19:14
    import and create a thousand different
  • 00:19:16
    user accounts using powershell and it's
  • 00:19:18
    relatively real world for the most part
  • 00:19:20
    go ahead and check that out if you want
  • 00:19:21
    to yeah that's my top 10 powershell
  • 00:19:23
    commands i hope that it was useful if
  • 00:19:25
    you thought it was useful please feel
  • 00:19:27
    free to like and subscribe i'm trying to
  • 00:19:28
    get to 10k subs so if you could sub i
  • 00:19:31
    would really appreciate a lot definitely
  • 00:19:33
    i respond to everybody's comments all
  • 00:19:35
    the time so if you want to leave
  • 00:19:36
    comments 100 i'm going to read it and
  • 00:19:38
    respond to it i also have patreon if you
  • 00:19:40
    feel like supporting me and i also do
  • 00:19:42
    mentoring as well you can check both of
  • 00:19:43
    those things out in the description but
  • 00:19:45
    otherwise yeah thank you so much for
  • 00:19:46
    watching this far and we will see you
  • 00:19:48
    next time bye
  • 00:19:54
    you
タグ
  • PowerShell
  • IT
  • cybersecurity
  • automation
  • scripting
  • programming
  • beginner commands