{"id":23491,"date":"2023-04-05T12:55:53","date_gmt":"2023-04-05T07:25:53","guid":{"rendered":"https:\/\/java2blog.com\/?p=23491"},"modified":"2023-04-16T19:11:48","modified_gmt":"2023-04-16T13:41:48","slug":"powershell-split-path-into-array","status":"publish","type":"post","link":"https:\/\/java2blog.com\/powershell-split-path-into-array\/","title":{"rendered":"PowerShell Split Path into Array"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Split_Path_into_Array_in_PowerShell\">Split Path into Array in PowerShell<\/a><ul><li><a href=\"#Using_Split_Method\">Using Split() Method<\/a><\/li><li><a href=\"#Using_-Split_Operator\">Using -Split Operator<\/a><\/li><\/ul><\/li><li><a href=\"#Split_Directory_into_Array_in_PowerShell\">Split Directory into Array in PowerShell<\/a><ul><li><a href=\"#Using_SystemIOPath_Class\">Using System.IO.Path Class<\/a><\/li><li><a href=\"#Using_Split-Path_Cmdlet\">Using Split-Path Cmdlet<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h2><span id=\"Split_Path_into_Array_in_PowerShell\">Split Path into Array in PowerShell<\/span><\/h2>\n<h3><span id=\"Using_Split_Method\">Using <code>Split()<\/code> Method<\/span><\/h3>\n<p><strong>Use the <code>Split()<\/code> method to split the path into an array in PowerShell.<\/strong><\/p>\n<pre code=\"powershell\" title=\"Use Split() Method\" mark=\"3\">\n$path = \"C:\\Users\\John\\Desktop\\Samples\\House_Prediction_Using_Two_File_Dataset.pdf\"\n$pathIntoArray = $path.Split(\"\\\")\n$pathIntoArray\n<\/pre>\n<pre title=\"OUTPUT\">\nC:\nUsers\nJohn\nDesktop\nSamples\nHouse_Prediction_Using_Two_File_Dataset.pdf\n<\/pre>\n<p>First, we defined a <code>$path<\/code> variable and set its value to a path as demonstrated above; you can initialize it with your own path. Next, we chained the <code>Split()<\/code> method with the <code>$path<\/code> variable and passed <code>\\<\/code> as a delimiter to the <code>Split()<\/code> method to split the <code>$path<\/code> into an array. Finally, we stored this array in the <code>$pathIntoArray<\/code> variable.<\/p>\n<p>We can use index operator to get elements of the array.<\/p>\n<pre code=\"powershell\" title=\"Get last element in the array\" mark=\"3\">\n$path = \"C:\\Users\\John\\Desktop\\Samples\\House_Prediction_Using_Two_File_Dataset.pdf\"\n$pathIntoArray = $path.Split(\"\\\")\n$last = ($pathIntoArray)[-1]\n$last\n<\/pre>\n<pre title=\"OUTPUT\">\nHouse_Prediction_Using_Two_File_Dataset.pdf\n<\/pre>\n<p>For example:<br \/>\nTo <a href=\"https:\/\/java2blog.com\/get-last-element-of-array-powershell\/\" title=\"get last element in array\">get last element in array<\/a>, we can use following code:<\/p>\n<h3><span id=\"Using_-Split_Operator\">Using <code>-Split<\/code> Operator<\/span><\/h3>\n<p><strong>Use the <code>-Split<\/code> operator to split the path into an array in PowerShell.<\/strong><\/p>\n<pre code=\"powershell\" title=\"Use -Split Operator\" mark=\"3\">\n$path = \"C:\\Users\\John\\Desktop\\Samples\\House_Prediction_Using_Two_File_Dataset.pdf\"\n$pathIntoArray = $path -Split \"\\\\\"\n$pathIntoArray\n<\/pre>\n<pre title=\"OUTPUT\">\nC:\nUsers\nJohn\nDesktop\nSamples\nHouse_Prediction_Using_Two_File_Dataset.pdf\n<\/pre>\n<p>This example is similar to the previous one, but we used the <code>-Split<\/code> operator this time, which took a <code>\\<\/code> as an argument. Note that the <code>\\<\/code> must be doubled while using the <code>-Split<\/code> operator. Also, unlike the previous code fence, we stored the array in the <code>$pathIntoArray<\/code> variable.<\/p>\n<p>You might wonder that if the <code>-Split<\/code> operator and <code>Split()<\/code> method perform the same thing, then why use them separately? You are correct that both split a string into an array of substrings, but some similarities and differences are listed below:<\/p>\n<ul>\n<li>First, they have different syntax because <code>Split()<\/code> is a method and <code>-Split<\/code> is an operator; you can refer to the last two examples to understand it.<\/li>\n<li>Both split the string based on the provided delimiter; however, the <code>-Split<\/code> operator can split based on the provided regular expressions, which makes it more powerful.<\/li>\n<li>Both returns an array of strings; you can check the returned type using <a href=\"https:\/\/java2blog.com\/get-object-type-powershell\/\">GetType()<\/a> method. For example, <code>$pathIntoArray.GetType()<\/code>.<\/li>\n<li>Regarding performance, the <code>Split()<\/code> method is faster than the <code>-Split<\/code> operator, particularly when we split based on a single delimiter. However, <code>-Split<\/code> can be faster when splitting based on complex patterns since it allows a regular expression pattern as a delimiter.<\/li>\n<\/ul>\n<p>Now, you know the similarities and differences between the <code>-Split<\/code> and <code>Split()<\/code>, so choose the one which best suits your project needs. <\/p>\n<p>Until now, we learned how to split the path of the specified file into an array, but what if we are supposed to split the directory and path of the given file into separate arrays? What does it mean? Suppose we have <strong>C:\\Users\\John\\Documents\\file.txt<\/strong> path. Splitting the path into an array will result in <code>C:<\/code>, <code>Users<\/code>, <code>John<\/code>, <code>Documents<\/code>, and <code>file.txt<\/code> as array elements, while splitting the directory will have <code>C:<\/code>, <code>Users<\/code>, <code>John<\/code>, and <code>Documents<\/code> as array elements.<\/p>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/split-text-file-on-newline-powershell\/\" target=\"_blank\" rel=\"noopener\">Split Text File on NewLine in PowerShell<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/split-text-file-on-newline-powershell\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/powershell-get-filename-from-path\/\" target=\"_blank\" rel=\"noopener\">PowerShell &#8211; Get Filename from Path<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/powershell-get-filename-from-path\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<p>How to split the directory into an array? Let&#8217;s learn it in the following section.<\/p>\n<h2><span id=\"Split_Directory_into_Array_in_PowerShell\">Split Directory into Array in PowerShell<\/span><\/h2>\n<h3><span id=\"Using_SystemIOPath_Class\">Using <code>System.IO.Path<\/code> Class<\/span><\/h3>\n<p>To split the directory and path of the specified file into separate arrays:<\/p>\n<ul>\n<li>Store a path of a file in a variable.<\/li>\n<li>Use <code>GetDirectoryName()<\/code> to retrieve the directory of the given file.<\/li>\n<li>Chain the  <code>Split()<\/code> method with the path variable created in the first step to get an array of the path.<\/li>\n<li>Chain the  <code>Split()<\/code> method with the directory variable created in the second step to get an array of the directory.<\/li>\n<li>Use the <code>Write-Host<\/code> cmdlet to print the customized outcomes.<\/li>\n<\/ul>\n<pre code=\"powershell\" title=\"Use System.IO.Path Class\">\n$path = \"C:\\Users\\John\\Desktop\\Samples\\House_Prediction_Using_Two_File_Dataset.pdf\"\n$directory = [System.IO.Path]::GetDirectoryName($path)\n$pathIntoArray = $path.Split(\"\\\")\n$directoryIntoArray = $directory.Split(\"\\\")\nWrite-Host \"A path into an array:`n$pathIntoArray\"\nWrite-Host \"A directory into an array:`n$directoryIntoArray\"\n<\/pre>\n<pre title=\"OUTPUT\">\nA path into an array:\nC: Users John Desktop Samples House_Prediction_Using_Two_File_Dataset.pdf\nA directory into an array:\nC: Users John Desktop Samples\n<\/pre>\n<p>In the above output, the array elements are separated by a single whitespace. So how did we get these arrays? First, we stored the path of the <code>House_Prediction_Using_Two_File_Dataset.pdf<\/code> file in the <code>$path<\/code> variable. Next, we used the <code>GetDirectoryName()<\/code> method of the <code>Path<\/code> class, provided <code>$path<\/code> as a parameter to get the directory of the <code>House_Prediction_Using_Two_File_Dataset.pdf<\/code> file, which we stored in the <code>$directory<\/code> variable.<\/p>\n<p>Next, we chained the <code>Split()<\/code> method with <code>$path<\/code> and <code>$directory<\/code> separately to get two different arrays stored in the <code>$pathIntoArray<\/code> and <code>$directoryIntoArray<\/code> variables. Remember, we passed <code>\\<\/code> to the <code>Split()<\/code> method as a delimiter. Finally, we used the <code>Write-Host<\/code> cmdlet to display customized output on the PowerShell terminal. <\/p>\n<blockquote>\n<p>`<code>n<\/code> is a unique character in PowerShell; we used it with the <code>Write-Host<\/code> cmdlet to add a new line character.<\/p>\n<\/blockquote>\n<p>Now, think of a situation where we want complete control over array elements. For example, we want to split the path into an array having parent, leaf, and qualifier as the array elements. What are they, and how can we do it? See the following section to learn.<\/p>\n<h3><span id=\"Using_Split-Path_Cmdlet\">Using <code>Split-Path<\/code> Cmdlet<\/span><\/h3>\n<p><strong>Use the <code>Split-Path<\/code> cmdlet to retrieve various parts of the path to form an array in PowerShell.<\/strong><\/p>\n<pre code=\"powershell\" title=\"Use Split-Path Cmdlet\">\n$path = \"C:\\Users\\John\\Desktop\\Samples\\House_Prediction_Using_Two_File_Dataset.pdf\"\n$pathParent = Split-Path $path -Parent\n$pathLeaf = Split-Path $path -Leaf\n$pathQualifier = Split-Path $path -Qualifier\n$pathIsAbsolute = Split-Path $path -IsAbsolute\n$pathIntoArray = @(\"Parent of the path: $pathParent\", \n                   \"Leaf of the path: $pathLeaf\",\n                   \"Qualifier of the path: $pathQualifier\",\n                   \"Is the path absolute? (True\/False): $pathIsAbsolute\")\n$pathIntoArray\n<\/pre>\n<pre title=\"OUTPUT\">\nParent of the path: C:\\Users\\John\\Desktop\\Samples\nLeaf of the path: House_Prediction_Using_Two_File_Dataset.pdf\nQualifier of the path: C:\nIs the path absolute? (True\/False): True\n<\/pre>\n<p>Again, we defined and initialized the <code>$path<\/code> variable. Then, we used <a href=\"https:\/\/learn.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.management\/split-path?view=powershell-7.3\" target=\"_blank\" rel=\"noopener\">Split-Path<\/a> cmdlet four times; every time with a different parameter to get the specified part of the <code>$path<\/code>. These parameters include <code>-Parent<\/code>, <code>-Leaf<\/code>, <code>-Qualifier<\/code>, and <code>-IsAbsolute<\/code>. Using the <code>-Parent<\/code> parameter, retrieved the parent folder of the <code>House_Prediction_Using_Two_File_Dataset.pdf<\/code> file. It is the default parameter, which means we can get the parent folder even if we omit the <code>-Parent<\/code> parameter.<\/p>\n<p>The <code>-Leaf<\/code> parameter provided us with the file name, while the <code>-Qualifier<\/code> retrieved the root directory of the specified file. Finally, the <code>-IsAbsolute<\/code> parameter returned a Boolean value, <code>True<\/code> if the path is an absolute path; otherwise, <code>False<\/code>. What is an absolute path?  It is a complete file path from the root to the current file, such as <strong>C:\\Users\\John\\Documents\\file.txt<\/strong>.<\/p>\n<p>For every parameter, we stored the returned value in a separate variable that is <code>$pathParent<\/code>, <code>$pathLeaf<\/code>, <code>$pathQualifier<\/code>, and <code>$pathIsAbsolute<\/code> that we enclosed within the array operator represented by <code>@()<\/code> to make an array from them. Finally, we stored this array in the <code>$pathIntoArray<\/code> variable.<\/p>\n<blockquote>\n<p>While using the array operator (<code>@()<\/code>), we wrote a customized message to make every array element more understandable.<\/p>\n<\/blockquote>\n<p>That&#8217;s split path into array in PowerShell.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsSplit Path into Array in PowerShellUsing Split() MethodUsing -Split OperatorSplit Directory into Array in PowerShellUsing System.IO.Path ClassUsing Split-Path Cmdlet Split Path into Array in PowerShell Using Split() Method Use the Split() method to split the path into an array in PowerShell. $path = &#8220;C:\\Users\\John\\Desktop\\Samples\\House_Prediction_Using_Two_File_Dataset.pdf&#8221; $pathIntoArray = $path.Split(&#8220;\\&#8221;) $pathIntoArray C: Users John Desktop Samples [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":23579,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[309,298],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/23491"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=23491"}],"version-history":[{"count":7,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/23491\/revisions"}],"predecessor-version":[{"id":23578,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/23491\/revisions\/23578"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/23579"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=23491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=23491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=23491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}