public static CommandResult BrowseFilesystem(string path, bool recurse, int depth, bool includeHidden, string searchPattern)
        {
            CommandResult results = new CommandResult();

            DirectoryInfo gciDir = new DirectoryInfo(path);

            if (!gciDir.Exists)
            {
                throw new ItemNotFoundException(path);
            }

            // TODO: Follow symlinks. Skipping them for now
            if ((gciDir.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
            {
                return(results);
            }

            DirectoryInfo[] directories;
            try
            {
                directories = gciDir.GetDirectories(recurse ? "*" : searchPattern);
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("Unauthorized to access \"{0}\"", path);
                return(results);
            }

            FileInfo[] files = gciDir.GetFiles(searchPattern);

            // Enumerate directories
            foreach (DirectoryInfo dir in directories)
            {
                if (!includeHidden && ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden))
                {
                    continue;
                }

                // Don't show directories if -Recurse and an -Include filter is set
                if (recurse && !string.IsNullOrEmpty(searchPattern))
                {
                    continue;
                }

                ResultRecord currentDir = new ResultRecord()
                {
                    { "Mode", GetModeFlags(dir) },
                    { "LastWriteTime", dir.LastWriteTime.ToString() },
                    { "Length", string.Empty },
                    { "Name", dir.Name }
                };

                // If recursive, also the directory name is needed
                if (recurse)
                {
                    currentDir.Add("Directory", dir.FullName);
                }

                results.Add(currentDir);
            }

            // Enumerate files
            foreach (FileInfo file in files)
            {
                if (!includeHidden && ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden))
                {
                    continue;
                }

                ResultRecord currentFile = new ResultRecord()
                {
                    { "Mode", GetModeFlags(file) },
                    { "LastWriteTime", file.LastWriteTime.ToString() },
                    { "Length", file.Length.ToString() },
                    { "Name", file.Name }
                };

                // If recursive, also the directory name is needed
                if (recurse)
                {
                    currentFile.Add("Directory", file.Directory.FullName);
                }

                results.Add(currentFile);
            }

            // After adding folders and files in current directory, go depth first
            if (recurse && depth > 0)
            {
                foreach (DirectoryInfo subDir in directories)
                {
                    // Skip hidden directories in case -Force parameter is not provided
                    if ((subDir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden && !includeHidden)
                    {
                        continue;
                    }

                    CommandResult currentDir = BrowseFilesystem(subDir.FullName, recurse, depth - 1, includeHidden, searchPattern);
                    results.AddRange(currentDir);
                }
            }

            return(results);
        }